blob: c20c65a5cea81c6f89aab1f00c37a17d08437e98 [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"
Chris Lattner20b1ea02001-09-14 03:47:57 +000019#include "llvm/DerivedTypes.h"
20#include "llvm/iTerminators.h"
21#include "llvm/iMemory.h"
22#include "llvm/iOther.h"
23#include "llvm/BasicBlock.h"
24#include "llvm/Method.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000025#include "llvm/ConstantVals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000027#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000028using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000029
30//************************* Forward Declarations ***************************/
31
32
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000033static void SetMemOperands_Internal (MachineInstr* minstr,
34 const InstructionNode* vmInstrNode,
35 Value* ptrVal,
36 Value* arrayOffsetVal,
Chris Lattner697954c2002-01-20 22:54:45 +000037 const std::vector<Value*>& idxVec,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000038 const TargetMachine& target);
Chris Lattner20b1ea02001-09-14 03:47:57 +000039
40
41//************************ Internal Functions ******************************/
42
Chris Lattner20b1ea02001-09-14 03:47:57 +000043
Chris Lattner20b1ea02001-09-14 03:47:57 +000044static inline MachineOpCode
45ChooseBprInstruction(const InstructionNode* instrNode)
46{
47 MachineOpCode opCode;
48
49 Instruction* setCCInstr =
50 ((InstructionNode*) instrNode->leftChild())->getInstruction();
51
52 switch(setCCInstr->getOpcode())
53 {
54 case Instruction::SetEQ: opCode = BRZ; break;
55 case Instruction::SetNE: opCode = BRNZ; break;
56 case Instruction::SetLE: opCode = BRLEZ; break;
57 case Instruction::SetGE: opCode = BRGEZ; break;
58 case Instruction::SetLT: opCode = BRLZ; break;
59 case Instruction::SetGT: opCode = BRGZ; break;
60 default:
61 assert(0 && "Unrecognized VM instruction!");
62 opCode = INVALID_OPCODE;
63 break;
64 }
65
66 return opCode;
67}
68
69
70static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000071ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000072 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000073{
74 MachineOpCode opCode = INVALID_OPCODE;
75
76 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
77
78 if (isSigned)
79 {
80 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000081 {
82 case Instruction::SetEQ: opCode = BE; break;
83 case Instruction::SetNE: opCode = BNE; break;
84 case Instruction::SetLE: opCode = BLE; break;
85 case Instruction::SetGE: opCode = BGE; break;
86 case Instruction::SetLT: opCode = BL; break;
87 case Instruction::SetGT: opCode = BG; break;
88 default:
89 assert(0 && "Unrecognized VM instruction!");
90 break;
91 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000092 }
93 else
94 {
95 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000096 {
97 case Instruction::SetEQ: opCode = BE; break;
98 case Instruction::SetNE: opCode = BNE; break;
99 case Instruction::SetLE: opCode = BLEU; break;
100 case Instruction::SetGE: opCode = BCC; break;
101 case Instruction::SetLT: opCode = BCS; break;
102 case Instruction::SetGT: opCode = BGU; break;
103 default:
104 assert(0 && "Unrecognized VM instruction!");
105 break;
106 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000107 }
108
109 return opCode;
110}
111
112static inline MachineOpCode
113ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000114 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000115{
116 MachineOpCode opCode = INVALID_OPCODE;
117
118 switch(setCCInstr->getOpcode())
119 {
120 case Instruction::SetEQ: opCode = FBE; break;
121 case Instruction::SetNE: opCode = FBNE; break;
122 case Instruction::SetLE: opCode = FBLE; break;
123 case Instruction::SetGE: opCode = FBGE; break;
124 case Instruction::SetLT: opCode = FBL; break;
125 case Instruction::SetGT: opCode = FBG; break;
126 default:
127 assert(0 && "Unrecognized VM instruction!");
128 break;
129 }
130
131 return opCode;
132}
133
134
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000135// Create a unique TmpInstruction for a boolean value,
136// representing the CC register used by a branch on that value.
137// For now, hack this using a little static cache of TmpInstructions.
138// Eventually the entire BURG instruction selection should be put
139// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000140// The static cache is not too bad because the memory for these
141// TmpInstructions will be freed along with the rest of the Method anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000142//
143static TmpInstruction*
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000144GetTmpForCC(Value* boolVal, const Method* method, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000145{
Chris Lattner697954c2002-01-20 22:54:45 +0000146 typedef std::hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000147 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
148 static const Method* lastMethod = NULL; // Use to flush cache between methods
149
150 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
151
152 if (lastMethod != method)
153 {
154 lastMethod = method;
155 boolToTmpCache.clear();
156 }
157
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000158 // Look for tmpI and create a new one otherwise. The new value is
159 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000160 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
161 if (tmpI == NULL)
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000162 tmpI = new TmpInstruction(TMP_INSTRUCTION_OPCODE, ccType, boolVal, NULL);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000163
164 return tmpI;
165}
166
167
Chris Lattner20b1ea02001-09-14 03:47:57 +0000168static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000169ChooseBccInstruction(const InstructionNode* instrNode,
170 bool& isFPBranch)
171{
172 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
173 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
174 const Type* setCCType = setCCInstr->getOperand(0)->getType();
175
176 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
177
178 if (isFPBranch)
179 return ChooseBFpccInstruction(instrNode, setCCInstr);
180 else
181 return ChooseBpccInstruction(instrNode, setCCInstr);
182}
183
184
185static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000186ChooseMovFpccInstruction(const InstructionNode* instrNode)
187{
188 MachineOpCode opCode = INVALID_OPCODE;
189
190 switch(instrNode->getInstruction()->getOpcode())
191 {
192 case Instruction::SetEQ: opCode = MOVFE; break;
193 case Instruction::SetNE: opCode = MOVFNE; break;
194 case Instruction::SetLE: opCode = MOVFLE; break;
195 case Instruction::SetGE: opCode = MOVFGE; break;
196 case Instruction::SetLT: opCode = MOVFL; break;
197 case Instruction::SetGT: opCode = MOVFG; break;
198 default:
199 assert(0 && "Unrecognized VM instruction!");
200 break;
201 }
202
203 return opCode;
204}
205
206
207// Assumes that SUBcc v1, v2 -> v3 has been executed.
208// In most cases, we want to clear v3 and then follow it by instruction
209// MOVcc 1 -> v3.
210// Set mustClearReg=false if v3 need not be cleared before conditional move.
211// Set valueToMove=0 if we want to conditionally move 0 instead of 1
212// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000213// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000214//
215static MachineOpCode
216ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000217 bool& mustClearReg,
218 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000219{
220 MachineOpCode opCode = INVALID_OPCODE;
221 mustClearReg = true;
222 valueToMove = 1;
223
224 switch(instrNode->getInstruction()->getOpcode())
225 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000226 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000227 case Instruction::SetLE: opCode = MOVLE; break;
228 case Instruction::SetGE: opCode = MOVGE; break;
229 case Instruction::SetLT: opCode = MOVL; break;
230 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000231 case Instruction::SetNE: assert(0 && "No move required!"); break;
232 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000233 }
234
235 return opCode;
236}
237
Chris Lattner20b1ea02001-09-14 03:47:57 +0000238static inline MachineOpCode
239ChooseConvertToFloatInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000240 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000241{
242 MachineOpCode opCode = INVALID_OPCODE;
243
244 switch(instrNode->getOpLabel())
245 {
246 case ToFloatTy:
247 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000248 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000249 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000250 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000251 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000252 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000253 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000254 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000255 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000256 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000257 break;
258
259 case ToDoubleTy:
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000260 // Use FXTOD for all integer-to-double conversions. This has to be
261 // consistent with the code in CreateCodeToCopyIntToFloat() since
262 // that will be used to load the integer into an FP register.
263 //
264 if (opType == Type::SByteTy || opType == Type::ShortTy ||
265 opType == Type::IntTy || opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000266 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000267 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000268 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000269 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000270 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000271 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000272 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000273 break;
274
275 default:
276 break;
277 }
278
279 return opCode;
280}
281
282static inline MachineOpCode
283ChooseConvertToIntInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000284 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000285{
286 MachineOpCode opCode = INVALID_OPCODE;;
287
288 int instrType = (int) instrNode->getOpLabel();
289
290 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
291 {
292 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000293 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000294 case Type::FloatTyID: opCode = FSTOI; break;
295 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000296 default:
297 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
298 break;
299 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000300 }
301 else if (instrType == ToLongTy)
302 {
303 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000304 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000305 case Type::FloatTyID: opCode = FSTOX; break;
306 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000307 default:
308 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
309 break;
310 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000311 }
312 else
313 assert(0 && "Should not get here, Mo!");
314
315 return opCode;
316}
317
318
319static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000320ChooseAddInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000321{
322 MachineOpCode opCode = INVALID_OPCODE;
323
Chris Lattner20b1ea02001-09-14 03:47:57 +0000324 if (resultType->isIntegral() ||
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000325 resultType->isPointerType() ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000326 resultType->isLabelType() ||
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000327 isa<MethodType>(resultType) ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000328 resultType == Type::BoolTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000329 {
330 opCode = ADD;
331 }
332 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000333 switch(resultType->getPrimitiveID())
334 {
335 case Type::FloatTyID: opCode = FADDS; break;
336 case Type::DoubleTyID: opCode = FADDD; break;
337 default: assert(0 && "Invalid type for ADD instruction"); break;
338 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000339
340 return opCode;
341}
342
343
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000344static inline MachineOpCode
345ChooseAddInstruction(const InstructionNode* instrNode)
346{
347 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
348}
349
350
Chris Lattner20b1ea02001-09-14 03:47:57 +0000351static inline MachineInstr*
352CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000353 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000354{
355 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000356 ? FMOVS : FMOVD);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000357 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000358 instrNode->leftChild()->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000359 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000360 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000361 return minstr;
362}
363
364static inline MachineInstr*
365CreateAddConstInstruction(const InstructionNode* instrNode)
366{
367 MachineInstr* minstr = NULL;
368
369 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000370 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000371
372 // Cases worth optimizing are:
373 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
374 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
375 //
376 const Type* resultType = instrNode->getInstruction()->getType();
377
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000378 if (resultType == Type::FloatTy ||
379 resultType == Type::DoubleTy)
380 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000381 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000382 if (dval == 0.0)
383 minstr = CreateMovFloatInstruction(instrNode, resultType);
384 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000385
386 return minstr;
387}
388
389
390static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000391ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000392{
393 MachineOpCode opCode = INVALID_OPCODE;
394
Chris Lattner20b1ea02001-09-14 03:47:57 +0000395 if (resultType->isIntegral() ||
396 resultType->isPointerType())
397 {
398 opCode = SUB;
399 }
400 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000401 switch(resultType->getPrimitiveID())
402 {
403 case Type::FloatTyID: opCode = FSUBS; break;
404 case Type::DoubleTyID: opCode = FSUBD; break;
405 default: assert(0 && "Invalid type for SUB instruction"); break;
406 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000407
408 return opCode;
409}
410
411
412static inline MachineInstr*
413CreateSubConstInstruction(const InstructionNode* instrNode)
414{
415 MachineInstr* minstr = NULL;
416
417 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000418 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000419
420 // Cases worth optimizing are:
421 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
422 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
423 //
424 const Type* resultType = instrNode->getInstruction()->getType();
425
426 if (resultType == Type::FloatTy ||
427 resultType == Type::DoubleTy)
428 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000429 double dval = cast<ConstantFP>(constOp)->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000430 if (dval == 0.0)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000431 minstr = CreateMovFloatInstruction(instrNode, resultType);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000432 }
433
434 return minstr;
435}
436
437
438static inline MachineOpCode
439ChooseFcmpInstruction(const InstructionNode* instrNode)
440{
441 MachineOpCode opCode = INVALID_OPCODE;
442
443 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
444 switch(operand->getType()->getPrimitiveID()) {
445 case Type::FloatTyID: opCode = FCMPS; break;
446 case Type::DoubleTyID: opCode = FCMPD; break;
447 default: assert(0 && "Invalid type for FCMP instruction"); break;
448 }
449
450 return opCode;
451}
452
453
454// Assumes that leftArg and rightArg are both cast instructions.
455//
456static inline bool
457BothFloatToDouble(const InstructionNode* instrNode)
458{
459 InstrTreeNode* leftArg = instrNode->leftChild();
460 InstrTreeNode* rightArg = instrNode->rightChild();
461 InstrTreeNode* leftArgArg = leftArg->leftChild();
462 InstrTreeNode* rightArgArg = rightArg->leftChild();
463 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
464
465 // Check if both arguments are floats cast to double
466 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000467 leftArgArg->getValue()->getType() == Type::FloatTy &&
468 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000469}
470
471
472static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000473ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000474{
475 MachineOpCode opCode = INVALID_OPCODE;
476
Chris Lattner20b1ea02001-09-14 03:47:57 +0000477 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000478 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000479 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000480 switch(resultType->getPrimitiveID())
481 {
482 case Type::FloatTyID: opCode = FMULS; break;
483 case Type::DoubleTyID: opCode = FMULD; break;
484 default: assert(0 && "Invalid type for MUL instruction"); break;
485 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000486
487 return opCode;
488}
489
490
Vikram S. Adve510eec72001-11-04 21:59:14 +0000491static inline MachineOpCode
492ChooseMulInstruction(const InstructionNode* instrNode,
493 bool checkCasts)
494{
495 if (checkCasts && BothFloatToDouble(instrNode))
496 return FSMULD;
497
498 // else use the regular multiply instructions
499 return ChooseMulInstructionByType(instrNode->getInstruction()->getType());
500}
501
502
Chris Lattner20b1ea02001-09-14 03:47:57 +0000503static 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{
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000520 MachineInstr* minstr = NULL; // return NULL if we cannot exploit constant
521 getMinstr2 = NULL; // to create a cheaper instruction
Chris Lattner20b1ea02001-09-14 03:47:57 +0000522
523 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000524 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000525
526 // Cases worth optimizing are:
527 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
528 // (2) Multiply by 2^x for integer types: replace with Shift
529 //
530 const Type* resultType = instrNode->getInstruction()->getType();
531
Vikram S. Adve243dd452001-09-18 13:03:13 +0000532 if (resultType->isIntegral() || resultType->isPointerType())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000533 {
534 unsigned pow;
535 bool isValidConst;
536 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
537 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000538 {
539 bool needNeg = false;
540 if (C < 0)
541 {
542 needNeg = true;
543 C = -C;
544 }
545
546 if (C == 0 || C == 1)
547 {
548 minstr = new MachineInstr(ADD);
549
550 if (C == 0)
551 minstr->SetMachineOperand(0,
552 target.getRegInfo().getZeroRegNum());
553 else
554 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
555 instrNode->leftChild()->getValue());
556 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
557 }
558 else if (IsPowerOf2(C, pow))
559 {
560 minstr = new MachineInstr((resultType == Type::LongTy)
561 ? SLLX : SLL);
562 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
563 instrNode->leftChild()->getValue());
564 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
565 pow);
566 }
567
568 if (minstr && needNeg)
569 { // insert <reg = SUB 0, reg> after the instr to flip the sign
570 getMinstr2 = CreateIntNegInstruction(target,
571 instrNode->getValue());
572 }
573 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000574 }
575 else
576 {
577 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000578 resultType == Type::DoubleTy)
579 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000580 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000581 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000582 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000583 bool needNeg = (dval < 0);
584
585 MachineOpCode opCode = needNeg
586 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
587 : (resultType == Type::FloatTy? FMOVS : FMOVD);
588
589 minstr = new MachineInstr(opCode);
590 minstr->SetMachineOperand(0,
591 MachineOperand::MO_VirtualRegister,
592 instrNode->leftChild()->getValue());
593 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000594 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000595 }
596
597 if (minstr != NULL)
598 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000599 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000600
601 return minstr;
602}
603
604
Vikram S. Adve510eec72001-11-04 21:59:14 +0000605// Generate a divide instruction for Div or Rem.
606// For Rem, this assumes that the operand type will be signed if the result
607// type is signed. This is correct because they must have the same sign.
608//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000609static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000610ChooseDivInstruction(TargetMachine &target,
611 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000612{
613 MachineOpCode opCode = INVALID_OPCODE;
614
615 const Type* resultType = instrNode->getInstruction()->getType();
616
617 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000618 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000619 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000620 switch(resultType->getPrimitiveID())
621 {
622 case Type::FloatTyID: opCode = FDIVS; break;
623 case Type::DoubleTyID: opCode = FDIVD; break;
624 default: assert(0 && "Invalid type for DIV instruction"); break;
625 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000626
627 return opCode;
628}
629
630
631static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000632CreateDivConstInstruction(TargetMachine &target,
633 const InstructionNode* instrNode,
634 MachineInstr*& getMinstr2)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000635{
636 MachineInstr* minstr = NULL;
637 getMinstr2 = NULL;
638
639 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000640 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000641
642 // Cases worth optimizing are:
643 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
644 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
645 //
646 const Type* resultType = instrNode->getInstruction()->getType();
647
648 if (resultType->isIntegral())
649 {
650 unsigned pow;
651 bool isValidConst;
652 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
653 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000654 {
655 bool needNeg = false;
656 if (C < 0)
657 {
658 needNeg = true;
659 C = -C;
660 }
661
662 if (C == 1)
663 {
664 minstr = new MachineInstr(ADD);
665 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
666 instrNode->leftChild()->getValue());
667 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
668 }
669 else if (IsPowerOf2(C, pow))
670 {
671 MachineOpCode opCode= ((resultType->isSigned())
672 ? (resultType==Type::LongTy)? SRAX : SRA
673 : (resultType==Type::LongTy)? SRLX : SRL);
674 minstr = new MachineInstr(opCode);
675 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
676 instrNode->leftChild()->getValue());
677 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
678 pow);
679 }
680
681 if (minstr && needNeg)
682 { // insert <reg = SUB 0, reg> after the instr to flip the sign
683 getMinstr2 = CreateIntNegInstruction(target,
684 instrNode->getValue());
685 }
686 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000687 }
688 else
689 {
690 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000691 resultType == Type::DoubleTy)
692 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000693 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000694 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000695 {
696 bool needNeg = (dval < 0);
697
698 MachineOpCode opCode = needNeg
699 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
700 : (resultType == Type::FloatTy? FMOVS : FMOVD);
701
702 minstr = new MachineInstr(opCode);
703 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
704 instrNode->leftChild()->getValue());
705 }
706 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000707 }
708
709 if (minstr != NULL)
710 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000711 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000712
713 return minstr;
714}
715
716
Chris Lattner20b1ea02001-09-14 03:47:57 +0000717//------------------------------------------------------------------------
718// Function SetOperandsForMemInstr
719//
720// Choose addressing mode for the given load or store instruction.
721// Use [reg+reg] if it is an indexed reference, and the index offset is
722// not a constant or if it cannot fit in the offset field.
723// Use [reg+offset] in all other cases.
724//
725// This assumes that all array refs are "lowered" to one of these forms:
726// %x = load (subarray*) ptr, constant ; single constant offset
727// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
728// Generally, this should happen via strength reduction + LICM.
729// Also, strength reduction should take care of using the same register for
730// the loop index variable and an array index, when that is profitable.
731//------------------------------------------------------------------------
732
733static void
734SetOperandsForMemInstr(MachineInstr* minstr,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000735 const InstructionNode* vmInstrNode,
736 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000737{
738 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
739
740 // Variables to hold the index vector, ptr value, and offset value.
741 // The major work here is to extract these for all 3 instruction types
742 // and then call the common function SetMemOperands_Internal().
743 //
Vikram S. Advefa248972001-12-15 00:36:32 +0000744 vector<Value*> idxVec;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000745 Value* ptrVal;
746 Value* arrayOffsetVal = NULL;
747
748 // Test if a GetElemPtr instruction is being folded into this mem instrn.
749 // If so, it will be in the left child for Load and GetElemPtr,
750 // and in the right child for Store instructions.
751 //
752 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000753 ? vmInstrNode->rightChild()
754 : vmInstrNode->leftChild());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000755
756 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
757 ptrChild->getOpLabel() == GetElemPtrIdx)
758 {
759 // There is a GetElemPtr instruction and there may be a chain of
760 // more than one. Use the pointer value of the last one in the chain.
761 // Fold the index vectors from the entire chain and from the mem
762 // instruction into one single index vector.
763 // Finally, we never fold for an array instruction so make that NULL.
764
Vikram S. Advefa248972001-12-15 00:36:32 +0000765 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, idxVec);
766 idxVec.insert(idxVec.end(), memInst->idx_begin(), memInst->idx_end());
Chris Lattner7a176752001-12-04 00:03:30 +0000767 assert(!((PointerType*)ptrVal->getType())->getElementType()->isArrayType()
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000768 && "GetElemPtr cannot be folded into array refs in selection");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000769 }
770 else
771 {
772 // There is no GetElemPtr instruction.
773 // Use the pointer value and the index vector from the Mem instruction.
Vikram S. Advefa248972001-12-15 00:36:32 +0000774 // If it is an array reference, check that it has been lowered to
775 // at most a single offset, then get the array offset value.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000776 //
Chris Lattner65ea1712001-11-14 11:27:58 +0000777 ptrVal = memInst->getPointerOperand();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000778
Chris Lattner7a176752001-12-04 00:03:30 +0000779 const Type* opType = cast<PointerType>(ptrVal->getType())->getElementType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000780 if (opType->isArrayType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000781 {
782 assert((memInst->getNumOperands()
Chris Lattner65ea1712001-11-14 11:27:58 +0000783 == (unsigned) 1 + memInst->getFirstIndexOperandNumber())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000784 && "Array refs must be lowered before Instruction Selection");
Vikram S. Advefa248972001-12-15 00:36:32 +0000785 arrayOffsetVal = * memInst->idx_begin();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000786 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000787 }
788
789 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
Vikram S. Advefa248972001-12-15 00:36:32 +0000790 idxVec, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000791}
792
793
794static void
795SetMemOperands_Internal(MachineInstr* minstr,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000796 const InstructionNode* vmInstrNode,
797 Value* ptrVal,
798 Value* arrayOffsetVal,
Vikram S. Advefa248972001-12-15 00:36:32 +0000799 const vector<Value*>& idxVec,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000800 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000801{
802 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
803
804 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000805 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000806 Value* valueForRegOffset = NULL;
807 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
808
809 // Check if there is an index vector and if so, if it translates to
810 // a small enough constant to fit in the immediate-offset field.
811 //
812 if (idxVec.size() > 0)
813 {
814 bool isConstantOffset = false;
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000815 unsigned offset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000816
817 const PointerType* ptrType = (PointerType*) ptrVal->getType();
818
Chris Lattner7a176752001-12-04 00:03:30 +0000819 if (ptrType->getElementType()->isStructType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000820 {
821 // the offset is always constant for structs
822 isConstantOffset = true;
823
824 // Compute the offset value using the index vector
825 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
826 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000827 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000828 {
829 // It must be an array ref. Check if the offset is a constant,
830 // and that the indexing has been lowered to a single offset.
831 //
Chris Lattner5618cb62001-12-14 16:31:26 +0000832 assert(isa<SequentialType>(ptrType->getElementType()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000833 assert(arrayOffsetVal != NULL
834 && "Expect to be given Value* for array offsets");
835
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000836 if (Constant *CPV = dyn_cast<Constant>(arrayOffsetVal))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000837 {
838 isConstantOffset = true; // always constant for structs
839 assert(arrayOffsetVal->getType()->isIntegral());
840 offset = (CPV->getType()->isSigned()
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000841 ? cast<ConstantSInt>(CPV)->getValue()
842 : (int64_t) cast<ConstantUInt>(CPV)->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000843 }
844 else
845 {
846 valueForRegOffset = arrayOffsetVal;
847 }
848 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000849
850 if (isConstantOffset)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000851 {
852 // create a virtual register for the constant
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000853 valueForRegOffset = ConstantSInt::get(Type::IntTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000854 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000855 }
856 else
857 {
858 offsetOpType = MachineOperand::MO_SignExtendedImmed;
859 smallConstOffset = 0;
860 }
861
862 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
863 // It is the left child in the instruction tree in all cases.
864 Value* leftVal = vmInstrNode->leftChild()->getValue();
865 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
866
867 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000868 // Operand 2 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
Chris Lattner20b1ea02001-09-14 03:47:57 +0000869 //
870 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
871 if (offsetOpType == MachineOperand::MO_VirtualRegister)
872 {
873 assert(valueForRegOffset != NULL);
874 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
875 }
876 else
877 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
878
879 if (memInst->getOpcode() == Instruction::Store)
880 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
881 else
882 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000883 vmInstrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000884}
885
886
Chris Lattner20b1ea02001-09-14 03:47:57 +0000887//
888// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +0000889// in place of the use(s) of that instruction in node `parent'.
890// Check both explicit and implicit operands!
Chris Lattner20b1ea02001-09-14 03:47:57 +0000891//
892static void
893ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000894 InstrTreeNode* parent,
895 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000896{
Vikram S. Adve243dd452001-09-18 13:03:13 +0000897 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
898
Chris Lattner20b1ea02001-09-14 03:47:57 +0000899 Instruction* unusedOp = treeNode->getInstruction();
900 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +0000901
902 // The parent itself may be a list node, so find the real parent instruction
903 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
904 {
905 parent = parent->parent();
906 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
907 }
908 InstructionNode* parentInstrNode = (InstructionNode*) parent;
909
910 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000911 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
912 for (unsigned i=0, N=mvec.size(); i < N; i++)
913 {
914 MachineInstr* minstr = mvec[i];
Vikram S. Advec025fc12001-10-14 23:28:43 +0000915
916 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000917 {
918 const MachineOperand& mop = minstr->getOperand(i);
919 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
920 mop.getVRegValue() == unusedOp)
921 {
922 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
923 fwdOp);
924 }
925 }
Vikram S. Advec025fc12001-10-14 23:28:43 +0000926
927 for (unsigned i=0, numOps=minstr->getNumImplicitRefs(); i < numOps; ++i)
928 if (minstr->getImplicitRef(i) == unusedOp)
929 minstr->setImplicitRef(i, fwdOp, minstr->implicitRefIsDefined(i));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000930 }
931}
932
933
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000934
935void UltraSparcInstrInfo::
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000936CreateCopyInstructionsByType(const TargetMachine& target,
937 Value* src,
938 Instruction* dest,
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000939 vector<MachineInstr*>& minstrVec) const
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000940{
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000941 bool loadConstantToReg = false;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000942
943 const Type* resultType = dest->getType();
944
945 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
946 if (opCode == INVALID_OPCODE)
947 {
948 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000949 return;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000950 }
951
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000952 // if `src' is a constant that doesn't fit in the immed field or if it is
953 // a global variable (i.e., a constant address), generate a load
954 // instruction instead of an add
955 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000956 if (isa<Constant>(src))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000957 {
958 unsigned int machineRegNum;
959 int64_t immedValue;
960 MachineOperand::MachineOperandType opType =
961 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
962 machineRegNum, immedValue);
963
964 if (opType == MachineOperand::MO_VirtualRegister)
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000965 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000966 }
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000967 else if (isa<GlobalValue>(src))
968 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000969
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000970 if (loadConstantToReg)
971 { // `src' is constant and cannot fit in immed field for the ADD
972 // Insert instructions to "load" the constant into a register
973 vector<TmpInstruction*> tempVec;
974 target.getInstrInfo().CreateCodeToLoadConst(src,dest,minstrVec,tempVec);
975 for (unsigned i=0; i < tempVec.size(); i++)
976 dest->getMachineInstrVec().addTempValue(tempVec[i]);
977 }
978 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000979 { // Create the appropriate add instruction.
980 // Make `src' the second operand, in case it is a constant
981 // Use (unsigned long) 0 for a NULL pointer value.
982 //
983 const Type* nullValueType =
984 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
985 : resultType;
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000986 MachineInstr* minstr = new MachineInstr(opCode);
987 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000988 Constant::getNullConstant(nullValueType));
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000989 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, src);
990 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
991 minstrVec.push_back(minstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000992 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000993}
994
995
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000996
Vikram S. Advefb361122001-10-22 13:36:31 +0000997//******************* Externally Visible Functions *************************/
998
999
1000//------------------------------------------------------------------------
1001// External Function: GetInstructionsForProlog
1002// External Function: GetInstructionsForEpilog
1003//
1004// Purpose:
1005// Create prolog and epilog code for procedure entry and exit
1006//------------------------------------------------------------------------
1007
1008extern unsigned
1009GetInstructionsForProlog(BasicBlock* entryBB,
1010 TargetMachine &target,
1011 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001012{
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001013 const MachineFrameInfo& frameInfo = target.getFrameInfo();
1014
Vikram S. Advefb361122001-10-22 13:36:31 +00001015 // The second operand is the stack size. If it does not fit in the
1016 // immediate field, we either have to find an unused register in the
1017 // caller's window or move some elements to the dynamically allocated
1018 // area of the stack frame (just above save area and method args).
1019 Method* method = entryBB->getParent();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001020 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
1021 unsigned int staticStackSize = mcInfo.getStaticStackSize();
1022
1023 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
1024 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
1025
1026 if (unsigned padsz = (staticStackSize %
1027 (unsigned) frameInfo.getStackFrameSizeAlignment()))
Vikram S. Advefd9b7dc2001-11-12 05:16:39 +00001028 staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001029
Vikram S. Advefb361122001-10-22 13:36:31 +00001030 assert(target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize)
1031 && "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 +00001032
Vikram S. Advefb361122001-10-22 13:36:31 +00001033 mvec[0] = new MachineInstr(SAVE);
1034 mvec[0]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1035 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001036 - (int) staticStackSize);
Vikram S. Advefb361122001-10-22 13:36:31 +00001037 mvec[0]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
1038
1039 return 1;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001040}
1041
1042
Vikram S. Advefb361122001-10-22 13:36:31 +00001043extern unsigned
1044GetInstructionsForEpilog(BasicBlock* anExitBB,
1045 TargetMachine &target,
1046 MachineInstr** mvec)
1047{
Vikram S. Advefb361122001-10-22 13:36:31 +00001048 mvec[0] = new MachineInstr(RESTORE);
1049 mvec[0]->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
Chris Lattner697954c2002-01-20 22:54:45 +00001050 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1051 (int64_t)0);
Vikram S. Advefb361122001-10-22 13:36:31 +00001052 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
1053
1054 return 1;
1055}
1056
1057
1058//------------------------------------------------------------------------
1059// External Function: ThisIsAChainRule
1060//
1061// Purpose:
1062// Check if a given BURG rule is a chain rule.
1063//------------------------------------------------------------------------
1064
1065extern bool
1066ThisIsAChainRule(int eruleno)
1067{
1068 switch(eruleno)
1069 {
1070 case 111: // stmt: reg
1071 case 113: // stmt: bool
1072 case 123:
1073 case 124:
1074 case 125:
1075 case 126:
1076 case 127:
1077 case 128:
1078 case 129:
1079 case 130:
1080 case 131:
1081 case 132:
1082 case 133:
1083 case 155:
1084 case 221:
1085 case 222:
1086 case 241:
1087 case 242:
1088 case 243:
1089 case 244:
1090 return true; break;
1091
1092 default:
1093 return false; break;
1094 }
1095}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001096
1097
1098//------------------------------------------------------------------------
1099// External Function: GetInstructionsByRule
1100//
1101// Purpose:
1102// Choose machine instructions for the SPARC according to the
1103// patterns chosen by the BURG-generated parser.
1104//------------------------------------------------------------------------
1105
1106unsigned
1107GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001108 int ruleForNode,
1109 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001110 TargetMachine &target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001111 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001112{
1113 int numInstr = 1; // initialize for common case
1114 bool checkCast = false; // initialize here to use fall-through
Chris Lattner20b1ea02001-09-14 03:47:57 +00001115 int nextRule;
1116 int forwardOperandNum = -1;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001117
Vikram S. Advefb361122001-10-22 13:36:31 +00001118 for (unsigned i=0; i < MAX_INSTR_PER_VMINSTR; i++)
1119 mvec[i] = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001120
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001121 //
1122 // Let's check for chain rules outside the switch so that we don't have
1123 // to duplicate the list of chain rule production numbers here again
1124 //
1125 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001126 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001127 // Chain rules have a single nonterminal on the RHS.
1128 // Get the rule that matches the RHS non-terminal and use that instead.
1129 //
1130 assert(nts[0] && ! nts[1]
1131 && "A chain rule should have only one RHS non-terminal!");
1132 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1133 nts = burm_nts[nextRule];
1134 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001135 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001136 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001137 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001138 switch(ruleForNode) {
1139 case 1: // stmt: Ret
1140 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001141 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001142 // for moving return value to appropriate register.
1143 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001144 // Mark the return value register as an implicit ref of
1145 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001146 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001147 ReturnInst *returnInstr =
1148 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001149 assert(returnInstr->getOpcode() == Instruction::Ret);
Vikram S. Advefb361122001-10-22 13:36:31 +00001150 Method* method = returnInstr->getParent()->getParent();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001151
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001152 Instruction* returnReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001153 returnInstr, NULL);
1154 returnInstr->getMachineInstrVec().addTempValue(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001155
1156 mvec[0] = new MachineInstr(JMPLRET);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001157 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1158 returnReg);
Chris Lattner697954c2002-01-20 22:54:45 +00001159 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1160 (int64_t)8);
Vikram S. Advefb361122001-10-22 13:36:31 +00001161 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001162
Vikram S. Advea995e602001-10-11 04:23:19 +00001163 if (returnInstr->getReturnValue() != NULL)
1164 mvec[0]->addImplicitRef(returnInstr->getReturnValue());
1165
Vikram S. Advefb361122001-10-22 13:36:31 +00001166 unsigned n = numInstr++; // delay slot
1167 mvec[n] = new MachineInstr(NOP);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001168
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001169 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001170 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001171
1172 case 3: // stmt: Store(reg,reg)
1173 case 4: // stmt: Store(reg,ptrreg)
1174 mvec[0] = new MachineInstr(
1175 ChooseStoreInstruction(
1176 subtreeRoot->leftChild()->getValue()->getType()));
1177 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1178 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001179
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001180 case 5: // stmt: BrUncond
1181 mvec[0] = new MachineInstr(BA);
1182 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1183 (Value*)NULL);
1184 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001185 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001186
1187 // delay slot
1188 mvec[numInstr++] = new MachineInstr(NOP);
1189 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001190
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001191 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001192 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001193 // If the constant is ZERO, we can use the branch-on-integer-register
1194 // instructions and avoid the SUBcc instruction entirely.
1195 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001196 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001197 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1198 assert(constNode &&
1199 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001200 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001201 bool isValidConst;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001202
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001203 if ((constVal->getType()->isIntegral()
1204 || constVal->getType()->isPointerType())
1205 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1206 && isValidConst)
1207 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001208 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1209
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001210 // That constant is a zero after all...
1211 // Use the left child of setCC as the first argument!
1212 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1213 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1214 subtreeRoot->leftChild()->leftChild()->getValue());
1215 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001216 brInst->getSuccessor(0));
Chris Lattner20b1ea02001-09-14 03:47:57 +00001217
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001218 // delay slot
1219 mvec[numInstr++] = new MachineInstr(NOP);
1220
1221 // false branch
1222 int n = numInstr++;
1223 mvec[n] = new MachineInstr(BA);
1224 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1225 (Value*) NULL);
1226 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001227 brInst->getSuccessor(1));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001228
1229 // delay slot
1230 mvec[numInstr++] = new MachineInstr(NOP);
1231
1232 break;
1233 }
1234 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001235 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001236
1237 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001238 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001239 // (SetCC, Not, ...). We need to check whether the type was a FP,
1240 // signed int or unsigned int, and check the branching condition in
1241 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001242 // If it is an integer CC, we also need to find the unique
1243 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001244 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001245 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001246 bool isFPBranch;
1247 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot,
1248 isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001249
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001250 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1251 brInst->getParent()->getParent(),
1252 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001253
1254 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, ccValue);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001255 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001256 brInst->getSuccessor(0));
1257
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001258 // delay slot
1259 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001260
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001261 // false branch
1262 int n = numInstr++;
1263 mvec[n] = new MachineInstr(BA);
1264 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1265 (Value*) NULL);
1266 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001267 brInst->getSuccessor(1));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001268
1269 // delay slot
1270 mvec[numInstr++] = new MachineInstr(NOP);
1271 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001272 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001273
1274 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001275 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001276 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001277 Constant* constVal =
1278 cast<Constant>(subtreeRoot->leftChild()->getValue());
1279 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001280
1281 mvec[0] = new MachineInstr(BA);
1282 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1283 (Value*) NULL);
1284 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1285 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
1286
1287 // delay slot
1288 mvec[numInstr++] = new MachineInstr(NOP);
1289 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001290 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001291
1292 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001293 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001294 // Just use the branch-on-integer-register instruction!
1295 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001296 mvec[0] = new MachineInstr(BRNZ);
1297 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1298 subtreeRoot->leftChild()->getValue());
1299 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1300 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1301
1302 // delay slot
1303 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1304
1305 // false branch
1306 int n = numInstr++;
1307 mvec[n] = new MachineInstr(BA);
1308 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1309 (Value*) NULL);
1310 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1311 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1312
1313 // delay slot
1314 mvec[numInstr++] = new MachineInstr(NOP);
1315 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001316 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001317
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001318 case 9: // stmt: Switch(reg)
1319 assert(0 && "*** SWITCH instruction is not implemented yet.");
1320 numInstr = 0;
1321 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001322
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001323 case 10: // reg: VRegList(reg, reg)
1324 assert(0 && "VRegList should never be the topmost non-chain rule");
1325 break;
1326
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001327 case 21: // bool: Not(bool): Both these are implemented as:
1328 case 321: // reg: BNot(reg) : reg = reg XOR-NOT 0
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001329 mvec[0] = new MachineInstr(XNOR);
1330 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1331 subtreeRoot->leftChild()->getValue());
1332 mvec[0]->SetMachineOperand(1, target.getRegInfo().getZeroRegNum());
1333 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1334 subtreeRoot->getValue());
1335 break;
1336
1337 case 322: // reg: ToBoolTy(bool):
1338 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001339 {
1340 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1341 assert(opType->isIntegral() || opType->isPointerType()
1342 || opType == Type::BoolTy);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001343 numInstr = 0;
1344 forwardOperandNum = 0;
1345 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001346 }
1347
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001348 case 23: // reg: ToUByteTy(reg)
1349 case 25: // reg: ToUShortTy(reg)
1350 case 27: // reg: ToUIntTy(reg)
1351 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001352 {
1353 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001354 assert(opType->isIntegral() ||
1355 opType->isPointerType() ||
1356 opType == Type::BoolTy && "Cast is illegal for other types");
1357 numInstr = 0;
1358 forwardOperandNum = 0;
1359 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001360 }
1361
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001362 case 24: // reg: ToSByteTy(reg)
1363 case 26: // reg: ToShortTy(reg)
1364 case 28: // reg: ToIntTy(reg)
1365 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001366 {
1367 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1368 if (opType->isIntegral()
1369 || opType->isPointerType()
1370 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001371 {
1372 numInstr = 0;
1373 forwardOperandNum = 0;
1374 }
1375 else
1376 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001377 // If the source operand is an FP type, the int result must be
1378 // copied from float to int register via memory!
1379 Instruction *dest = subtreeRoot->getInstruction();
1380 Value* leftVal = subtreeRoot->leftChild()->getValue();
1381 Value* destForCast;
1382 vector<MachineInstr*> minstrVec;
1383
1384 if (opType == Type::FloatTy || opType == Type::DoubleTy)
1385 {
1386 // Create a temporary to represent the INT register
1387 // into which the FP value will be copied via memory.
1388 // The type of this temporary will determine the FP
1389 // register used: single-prec for a 32-bit int or smaller,
1390 // double-prec for a 64-bit int.
1391 //
1392 const Type* destTypeToUse =
1393 (dest->getType() == Type::LongTy)? Type::DoubleTy
1394 : Type::FloatTy;
1395 destForCast = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1396 destTypeToUse, leftVal, NULL);
1397 dest->getMachineInstrVec().addTempValue(destForCast);
1398
1399 vector<TmpInstruction*> tempVec;
1400 target.getInstrInfo().CreateCodeToCopyFloatToInt(
1401 dest->getParent()->getParent(),
1402 (TmpInstruction*) destForCast, dest,
1403 minstrVec, tempVec, target);
1404
1405 for (unsigned i=0; i < tempVec.size(); ++i)
1406 dest->getMachineInstrVec().addTempValue(tempVec[i]);
1407 }
1408 else
1409 destForCast = leftVal;
1410
1411 MachineOpCode opCode=ChooseConvertToIntInstr(subtreeRoot, opType);
1412 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
1413
1414 mvec[0] = new MachineInstr(opCode);
1415 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1416 leftVal);
1417 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1418 destForCast);
1419
1420 assert(numInstr == 1 && "Should be initialized to 1 at the top");
1421 for (unsigned i=0; i < minstrVec.size(); ++i)
1422 mvec[numInstr++] = minstrVec[i];
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001423 }
1424 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001425 }
1426
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001427 case 31: // reg: ToFloatTy(reg):
1428 case 32: // reg: ToDoubleTy(reg):
1429 case 232: // reg: ToDoubleTy(Constant):
1430
1431 // If this instruction has a parent (a user) in the tree
1432 // and the user is translated as an FsMULd instruction,
1433 // then the cast is unnecessary. So check that first.
1434 // In the future, we'll want to do the same for the FdMULq instruction,
1435 // so do the check here instead of only for ToFloatTy(reg).
1436 //
1437 if (subtreeRoot->parent() != NULL &&
1438 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1439 {
1440 numInstr = 0;
1441 forwardOperandNum = 0;
1442 }
1443 else
1444 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001445 Value* leftVal = subtreeRoot->leftChild()->getValue();
1446 const Type* opType = leftVal->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001447 MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType);
1448 if (opCode == INVALID_OPCODE) // no conversion needed
1449 {
1450 numInstr = 0;
1451 forwardOperandNum = 0;
1452 }
1453 else
1454 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001455 // If the source operand is a non-FP type it must be
1456 // first copied from int to float register via memory!
1457 Instruction *dest = subtreeRoot->getInstruction();
1458 Value* srcForCast;
1459 int n = 0;
1460 if (opType != Type::FloatTy && opType != Type::DoubleTy)
1461 {
1462 // Create a temporary to represent the FP register
1463 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001464 // The type of this temporary will determine the FP
1465 // register used: single-prec for a 32-bit int or smaller,
1466 // double-prec for a 64-bit int.
1467 //
1468 const Type* srcTypeToUse =
1469 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1470 : Type::FloatTy;
1471
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001472 srcForCast = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001473 srcTypeToUse, dest, NULL);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001474 dest->getMachineInstrVec().addTempValue(srcForCast);
1475
1476 vector<MachineInstr*> minstrVec;
1477 vector<TmpInstruction*> tempVec;
1478 target.getInstrInfo().CreateCodeToCopyIntToFloat(
1479 dest->getParent()->getParent(),
1480 leftVal, (TmpInstruction*) srcForCast,
1481 minstrVec, tempVec, target);
1482
1483 for (unsigned i=0; i < minstrVec.size(); ++i)
1484 mvec[n++] = minstrVec[i];
1485
1486 for (unsigned i=0; i < tempVec.size(); ++i)
1487 dest->getMachineInstrVec().addTempValue(tempVec[i]);
1488 }
1489 else
1490 srcForCast = leftVal;
1491
1492 MachineInstr* castI = new MachineInstr(opCode);
1493 castI->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1494 srcForCast);
1495 castI->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1496 dest);
1497 mvec[n++] = castI;
1498 numInstr = n;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001499 }
1500 }
1501 break;
1502
1503 case 19: // reg: ToArrayTy(reg):
1504 case 20: // reg: ToPointerTy(reg):
1505 numInstr = 0;
1506 forwardOperandNum = 0;
1507 break;
1508
1509 case 233: // reg: Add(reg, Constant)
1510 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1511 if (mvec[0] != NULL)
1512 break;
1513 // ELSE FALL THROUGH
1514
1515 case 33: // reg: Add(reg, reg)
1516 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
1517 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1518 break;
1519
1520 case 234: // reg: Sub(reg, Constant)
1521 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1522 if (mvec[0] != NULL)
1523 break;
1524 // ELSE FALL THROUGH
1525
1526 case 34: // reg: Sub(reg, reg)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001527 mvec[0] = new MachineInstr(ChooseSubInstructionByType(
1528 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001529 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1530 break;
1531
1532 case 135: // reg: Mul(todouble, todouble)
1533 checkCast = true;
1534 // FALL THROUGH
1535
1536 case 35: // reg: Mul(reg, reg)
1537 mvec[0] =new MachineInstr(ChooseMulInstruction(subtreeRoot,checkCast));
1538 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1539 break;
1540
1541 case 335: // reg: Mul(todouble, todoubleConst)
1542 checkCast = true;
1543 // FALL THROUGH
1544
1545 case 235: // reg: Mul(reg, Constant)
1546 mvec[0] = CreateMulConstInstruction(target, subtreeRoot, mvec[1]);
1547 if (mvec[0] == NULL)
1548 {
1549 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot,
1550 checkCast));
1551 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1552 }
1553 else
1554 if (mvec[1] != NULL)
1555 ++numInstr;
1556 break;
1557
1558 case 236: // reg: Div(reg, Constant)
1559 mvec[0] = CreateDivConstInstruction(target, subtreeRoot, mvec[1]);
1560 if (mvec[0] != NULL)
1561 {
1562 if (mvec[1] != NULL)
1563 ++numInstr;
1564 }
1565 else
1566 // ELSE FALL THROUGH
1567
1568 case 36: // reg: Div(reg, reg)
1569 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1570 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1571 break;
1572
1573 case 37: // reg: Rem(reg, reg)
1574 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001575 {
1576 Instruction* remInstr = subtreeRoot->getInstruction();
1577
1578 TmpInstruction* quot = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1579 subtreeRoot->leftChild()->getValue(),
1580 subtreeRoot->rightChild()->getValue());
1581 TmpInstruction* prod = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1582 quot,
1583 subtreeRoot->rightChild()->getValue());
1584 remInstr->getMachineInstrVec().addTempValue(quot);
1585 remInstr->getMachineInstrVec().addTempValue(prod);
1586
1587 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1588 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1589 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,quot);
1590
1591 int n = numInstr++;
1592 mvec[n] = new MachineInstr(ChooseMulInstructionByType(
1593 subtreeRoot->getInstruction()->getType()));
1594 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,quot);
1595 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1596 subtreeRoot->rightChild()->getValue());
1597 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,prod);
1598
1599 n = numInstr++;
1600 mvec[n] = new MachineInstr(ChooseSubInstructionByType(
1601 subtreeRoot->getInstruction()->getType()));
1602 Set3OperandsFromInstr(mvec[n], subtreeRoot, target);
1603 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,prod);
1604
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001605 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001606 }
1607
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001608 case 38: // bool: And(bool, bool)
1609 case 238: // bool: And(bool, boolconst)
1610 case 338: // reg : BAnd(reg, reg)
1611 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001612 mvec[0] = new MachineInstr(AND);
1613 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1614 break;
1615
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001616 case 138: // bool: And(bool, not)
1617 case 438: // bool: BAnd(bool, not)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001618 mvec[0] = new MachineInstr(ANDN);
1619 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1620 break;
1621
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001622 case 39: // bool: Or(bool, bool)
1623 case 239: // bool: Or(bool, boolconst)
1624 case 339: // reg : BOr(reg, reg)
1625 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001626 mvec[0] = new MachineInstr(ORN);
1627 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1628 break;
1629
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001630 case 139: // bool: Or(bool, not)
1631 case 439: // bool: BOr(bool, not)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001632 mvec[0] = new MachineInstr(ORN);
1633 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1634 break;
1635
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001636 case 40: // bool: Xor(bool, bool)
1637 case 240: // bool: Xor(bool, boolconst)
1638 case 340: // reg : BXor(reg, reg)
1639 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001640 mvec[0] = new MachineInstr(XOR);
1641 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1642 break;
1643
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001644 case 140: // bool: Xor(bool, not)
1645 case 440: // bool: BXor(bool, not)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001646 mvec[0] = new MachineInstr(XNOR);
1647 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1648 break;
1649
1650 case 41: // boolconst: SetCC(reg, Constant)
1651 // Check if this is an integer comparison, and
1652 // there is a parent, and the parent decided to use
1653 // a branch-on-integer-register instead of branch-on-condition-code.
1654 // If so, the SUBcc instruction is not required.
1655 // (However, we must still check for constants to be loaded from
1656 // the constant pool so that such a load can be associated with
1657 // this instruction.)
1658 //
1659 // Otherwise this is just the same as case 42, so just fall through.
1660 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001661 if ((subtreeRoot->leftChild()->getValue()->getType()->isIntegral() ||
1662 subtreeRoot->leftChild()->getValue()->getType()->isPointerType())
1663 && subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001664 {
1665 InstructionNode* parent = (InstructionNode*) subtreeRoot->parent();
1666 assert(parent->getNodeType() == InstrTreeNode::NTInstructionNode);
1667 const vector<MachineInstr*>&
1668 minstrVec = parent->getInstruction()->getMachineInstrVec();
1669 MachineOpCode parentOpCode;
1670 if (parent->getInstruction()->getOpcode() == Instruction::Br &&
1671 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1672 parentOpCode <= BRGEZ)
1673 {
1674 numInstr = 0; // don't forward the operand!
1675 break;
1676 }
1677 }
1678 // ELSE FALL THROUGH
1679
1680 case 42: // bool: SetCC(reg, reg):
1681 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001682 // This generates a SUBCC instruction, putting the difference in
1683 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001684 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001685 // If the boolean result of the SetCC is used by anything other
1686 // than a single branch instruction, the boolean must be
1687 // computed and stored in the result register. Otherwise, discard
1688 // the difference (by using %g0) and keep only the condition code.
1689 //
1690 // To compute the boolean result in a register we use a conditional
1691 // move, unless the result of the SUBCC instruction can be used as
1692 // the bool! This assumes that zero is FALSE and any non-zero
1693 // integer is TRUE.
1694 //
1695 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1696 Instruction* setCCInstr = subtreeRoot->getInstruction();
1697 bool keepBoolVal = (parentNode == NULL ||
1698 parentNode->getInstruction()->getOpcode()
1699 != Instruction::Br);
1700 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001701 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1702 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1703
1704 bool mustClearReg;
1705 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001706 MachineOpCode movOpCode = 0;
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001707
1708 // Mark the 4th operand as being a CC register, and as a def
1709 // A TmpInstruction is created to represent the CC "result".
1710 // Unlike other instances of TmpInstruction, this one is used
1711 // by machine code of multiple LLVM instructions, viz.,
1712 // the SetCC and the branch. Make sure to get the same one!
1713 // Note that we do this even for FP CC registers even though they
1714 // are explicit operands, because the type of the operand
1715 // needs to be a floating point condition code, not an integer
1716 // condition code. Think of this as casting the bool result to
1717 // a FP condition code register.
1718 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001719 Value* leftVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001720 bool isFPCompare = (leftVal->getType() == Type::FloatTy ||
1721 leftVal->getType() == Type::DoubleTy);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001722
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001723 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1724 setCCInstr->getParent()->getParent(),
1725 isFPCompare? Type::FloatTy : Type::IntTy);
1726 setCCInstr->getMachineInstrVec().addTempValue(tmpForCC);
1727
1728 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001729 {
1730 // Integer condition: dest. should be %g0 or an integer register.
1731 // If result must be saved but condition is not SetEQ then we need
1732 // a separate instruction to compute the bool result, so discard
1733 // result of SUBcc instruction anyway.
1734 //
1735 mvec[0] = new MachineInstr(SUBcc);
1736 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, ! keepSubVal);
1737
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001738 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001739 tmpForCC, /*def*/true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001740
1741 if (computeBoolVal)
1742 { // recompute bool using the integer condition codes
1743 movOpCode =
1744 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1745 }
1746 }
1747 else
1748 {
1749 // FP condition: dest of FCMP should be some FCCn register
1750 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001751 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001752 tmpForCC);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001753 mvec[0]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
1754 subtreeRoot->leftChild()->getValue());
1755 mvec[0]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,
1756 subtreeRoot->rightChild()->getValue());
1757
1758 if (computeBoolVal)
1759 {// recompute bool using the FP condition codes
1760 mustClearReg = true;
1761 valueToMove = 1;
1762 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1763 }
1764 }
1765
1766 if (computeBoolVal)
1767 {
1768 if (mustClearReg)
1769 {// Unconditionally set register to 0
1770 int n = numInstr++;
1771 mvec[n] = new MachineInstr(SETHI);
1772 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001773 (int64_t)0);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001774 mvec[n]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001775 setCCInstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001776 }
1777
1778 // Now conditionally move `valueToMove' (0 or 1) into the register
1779 int n = numInstr++;
1780 mvec[n] = new MachineInstr(movOpCode);
1781 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001782 tmpForCC);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001783 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1784 valueToMove);
1785 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001786 setCCInstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001787 }
1788 break;
1789 }
1790
1791 case 43: // boolreg: VReg
1792 case 44: // boolreg: Constant
1793 numInstr = 0;
1794 break;
1795
1796 case 51: // reg: Load(reg)
1797 case 52: // reg: Load(ptrreg)
1798 case 53: // reg: LoadIdx(reg,reg)
1799 case 54: // reg: LoadIdx(ptrreg,reg)
1800 mvec[0] = new MachineInstr(ChooseLoadInstruction(
1801 subtreeRoot->getValue()->getType()));
1802 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1803 break;
1804
1805 case 55: // reg: GetElemPtr(reg)
1806 case 56: // reg: GetElemPtrIdx(reg,reg)
1807 if (subtreeRoot->parent() != NULL)
1808 {
Vikram S. Adve671b16d2001-11-10 01:05:26 +00001809 // If the parent was a memory operation and not an array access,
1810 // the parent will fold this instruction in so generate nothing.
1811 //
1812 Instruction* parent =
1813 cast<Instruction>(subtreeRoot->parent()->getValue());
1814 if (parent->getOpcode() == Instruction::Load ||
1815 parent->getOpcode() == Instruction::Store ||
1816 parent->getOpcode() == Instruction::GetElementPtr)
1817 {
1818 // Check if the parent is an array access,
1819 // If so, we still need to generate this instruction.
1820 GetElementPtrInst* getElemInst =
1821 cast<GetElementPtrInst>(subtreeRoot->getInstruction());
1822 const PointerType* ptrType =
Chris Lattner65ea1712001-11-14 11:27:58 +00001823 cast<PointerType>(getElemInst->getPointerOperand()->getType());
Chris Lattner7a176752001-12-04 00:03:30 +00001824 if (! ptrType->getElementType()->isArrayType())
Vikram S. Adve671b16d2001-11-10 01:05:26 +00001825 {// we don't need a separate instr
1826 numInstr = 0; // don't forward operand!
1827 break;
1828 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001829 }
1830 }
1831 // else in all other cases we need to a separate ADD instruction
1832 mvec[0] = new MachineInstr(ADD);
1833 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1834 break;
1835
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001836 case 57: // reg: Alloca: Implement as 1 instruction:
1837 { // add %fp, offsetFromFP -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001838 Instruction* instr = subtreeRoot->getInstruction();
1839 const PointerType* instrType = (const PointerType*) instr->getType();
1840 assert(instrType->isPointerType());
1841 int tsize = (int)
Chris Lattner7a176752001-12-04 00:03:30 +00001842 target.findOptimalStorageSize(instrType->getElementType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001843 assert(tsize != 0 && "Just to check when this can happen");
1844
Vikram S. Advefb361122001-10-22 13:36:31 +00001845 Method* method = instr->getParent()->getParent();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001846 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
Vikram S. Adve345bcc82001-11-15 15:22:39 +00001847 int offsetFromFP = mcInfo.allocateLocalVar(target, instr, (unsigned int) tsize);
Vikram S. Advefb361122001-10-22 13:36:31 +00001848
1849 // Create a temporary Value to hold the constant offset.
1850 // This is needed because it may not fit in the immediate field.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001851 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
Vikram S. Advefb361122001-10-22 13:36:31 +00001852
1853 // Instruction 1: add %fp, offsetFromFP -> result
1854 mvec[0] = new MachineInstr(ADD);
1855 mvec[0]->SetMachineOperand(0, target.getRegInfo().getFramePointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001856 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001857 offsetVal);
1858 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001859 instr);
1860 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001861 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001862
1863 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1864 // mul num, typeSz -> tmp
1865 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001866 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001867 Instruction* instr = subtreeRoot->getInstruction();
1868 const PointerType* instrType = (const PointerType*) instr->getType();
1869 assert(instrType->isPointerType() &&
Chris Lattner7a176752001-12-04 00:03:30 +00001870 instrType->getElementType()->isArrayType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001871 const Type* eltType =
Chris Lattner7a176752001-12-04 00:03:30 +00001872 ((ArrayType*) instrType->getElementType())->getElementType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001873 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefb361122001-10-22 13:36:31 +00001874
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001875 assert(tsize != 0 && "Just to check when this can happen");
Vikram S. Advefb361122001-10-22 13:36:31 +00001876
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001877 // Create a temporary Value to hold the constant type-size
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001878 ConstantSInt* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
Vikram S. Advefb361122001-10-22 13:36:31 +00001879
1880 // Create a temporary Value to hold the constant offset from SP
1881 Method* method = instr->getParent()->getParent();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001882 bool ignore; // we don't need this
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001883 ConstantSInt* dynamicAreaOffset = ConstantSInt::get(Type::IntTy,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001884 target.getFrameInfo().getDynamicAreaOffset(MachineCodeForMethod::get(method),
1885 ignore));
Vikram S. Advefb361122001-10-22 13:36:31 +00001886
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001887 // Create a temporary value to hold `tmp'
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001888 Instruction* tmpInstr = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001889 subtreeRoot->leftChild()->getValue(),
1890 NULL /*could insert tsize here*/);
1891 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1892
1893 // Instruction 1: mul numElements, typeSize -> tmp
1894 mvec[0] = new MachineInstr(MULX);
1895 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001896 subtreeRoot->leftChild()->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001897 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001898 tsizeVal);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001899 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1900 tmpInstr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001901
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001902 // Instruction 2: sub %sp, tmp -> %sp
1903 numInstr++;
1904 mvec[1] = new MachineInstr(SUB);
Vikram S. Advefb361122001-10-22 13:36:31 +00001905 mvec[1]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001906 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1907 tmpInstr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001908 mvec[1]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001909
Vikram S. Advefb361122001-10-22 13:36:31 +00001910 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001911 numInstr++;
1912 mvec[2] = new MachineInstr(ADD);
Vikram S. Advefb361122001-10-22 13:36:31 +00001913 mvec[2]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1914 mvec[2]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001915 dynamicAreaOffset);
Vikram S. Advefb361122001-10-22 13:36:31 +00001916 mvec[2]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,instr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001917 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001918 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001919
1920 case 61: // reg: Call
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001921 { // Generate a call-indirect (i.e., jmpl) for now to expose
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001922 // the potential need for registers. If an absolute address
1923 // is available, replace this with a CALL instruction.
1924 // Mark both the indirection register and the return-address
1925 // register as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00001926 // Also, mark the operands of the Call and return value (if
1927 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001928 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001929 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001930 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001931
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001932 Instruction* retAddrReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001933 callInstr, NULL);
Vikram S. Adve8557b222001-10-10 20:56:33 +00001934
Vikram S. Advea995e602001-10-11 04:23:19 +00001935 // Note temporary values in the machineInstrVec for the VM instr.
Vikram S. Adve8557b222001-10-10 20:56:33 +00001936 //
1937 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
1938 // The result value must go in slot N. This is assumed
1939 // in register allocation.
1940 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001941 callInstr->getMachineInstrVec().addTempValue(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001942
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001943
1944 // Generate the machine instruction and its operands.
1945 // Use CALL for direct function calls; this optimistically assumes
1946 // the PC-relative address fits in the CALL address field (22 bits).
1947 // Use JMPL for indirect calls.
1948 //
1949 if (callee->getValueType() == Value::MethodVal)
1950 { // direct function call
1951 mvec[0] = new MachineInstr(CALL);
1952 mvec[0]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1953 callee);
1954 }
1955 else
1956 { // indirect function call
Vikram S. Advefb361122001-10-22 13:36:31 +00001957 mvec[0] = new MachineInstr(JMPLCALL);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001958 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1959 callee);
1960 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1961 (int64_t) 0);
1962 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1963 retAddrReg);
1964 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001965
Vikram S. Advea995e602001-10-11 04:23:19 +00001966 // Add the call operands and return value as implicit refs
1967 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
1968 if (callInstr->getOperand(i) != callee)
1969 mvec[0]->addImplicitRef(callInstr->getOperand(i));
1970
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001971 if (callInstr->getType() != Type::VoidTy)
Vikram S. Advea995e602001-10-11 04:23:19 +00001972 mvec[0]->addImplicitRef(callInstr, /*isDef*/ true);
1973
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001974 // For the CALL instruction, the ret. addr. reg. is also implicit
1975 if (callee->getValueType() == Value::MethodVal)
1976 mvec[0]->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001977
1978 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1979 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001980 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001981
1982 case 62: // reg: Shl(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001983 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001984 assert(opType->isIntegral()
1985 || opType == Type::BoolTy
1986 || opType->isPointerType()&& "Shl unsupported for other types");
1987 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
1988 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1989 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001990 }
1991
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001992 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001993 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001994 assert(opType->isIntegral()
1995 || opType == Type::BoolTy
1996 || opType->isPointerType() &&"Shr unsupported for other types");
1997 mvec[0] = new MachineInstr((opType->isSigned()
1998 ? ((opType == Type::LongTy)? SRAX : SRA)
1999 : ((opType == Type::LongTy)? SRLX : SRL)));
2000 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
2001 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002002 }
2003
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002004 case 64: // reg: Phi(reg,reg)
Vikram S. Adve3438b212001-11-12 18:54:11 +00002005 numInstr = 0; // don't forward the value
2006 break;
2007#undef NEED_PHI_MACHINE_INSTRS
2008#ifdef NEED_PHI_MACHINE_INSTRS
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002009 { // This instruction has variable #operands, so resultPos is 0.
2010 Instruction* phi = subtreeRoot->getInstruction();
2011 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
2012 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
2013 subtreeRoot->getValue());
2014 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
2015 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
2016 phi->getOperand(i));
2017 break;
2018 }
Chris Lattner697954c2002-01-20 22:54:45 +00002019#endif // NEED_PHI_MACHINE_INSTRS
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002020
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002021 case 71: // reg: VReg
2022 case 72: // reg: Constant
2023 numInstr = 0; // don't forward the value
2024 break;
2025
2026 default:
2027 assert(0 && "Unrecognized BURG rule");
2028 numInstr = 0;
2029 break;
2030 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002031 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002032
2033 if (forwardOperandNum >= 0)
2034 { // We did not generate a machine instruction but need to use operand.
2035 // If user is in the same tree, replace Value in its machine operand.
2036 // If not, insert a copy instruction which should get coalesced away
2037 // by register allocation.
2038 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002039 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002040 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002041 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002042 vector<MachineInstr*> minstrVec;
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00002043 target.getInstrInfo().CreateCopyInstructionsByType(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002044 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002045 subtreeRoot->getInstruction(), minstrVec);
2046 assert(minstrVec.size() > 0);
2047 for (unsigned i=0; i < minstrVec.size(); ++i)
2048 mvec[numInstr++] = minstrVec[i];
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002049 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002050 }
2051
Chris Lattner20b1ea02001-09-14 03:47:57 +00002052 return numInstr;
2053}
2054
2055