blob: 6c5caaf6bfff3c68c8fdd429a92dab7ccf8e8318 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000011#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner9c461082002-02-03 07:50:56 +000015#include "llvm/CodeGen/MachineCodeForMethod.h"
16#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
20#include "llvm/iOther.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000021#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000022#include "llvm/Constants.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000024#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000026
27//************************* Forward Declarations ***************************/
28
29
Chris Lattner20b1ea02001-09-14 03:47:57 +000030//************************ Internal Functions ******************************/
31
Chris Lattner20b1ea02001-09-14 03:47:57 +000032
Chris Lattner20b1ea02001-09-14 03:47:57 +000033static inline MachineOpCode
34ChooseBprInstruction(const InstructionNode* instrNode)
35{
36 MachineOpCode opCode;
37
38 Instruction* setCCInstr =
39 ((InstructionNode*) instrNode->leftChild())->getInstruction();
40
41 switch(setCCInstr->getOpcode())
42 {
43 case Instruction::SetEQ: opCode = BRZ; break;
44 case Instruction::SetNE: opCode = BRNZ; break;
45 case Instruction::SetLE: opCode = BRLEZ; break;
46 case Instruction::SetGE: opCode = BRGEZ; break;
47 case Instruction::SetLT: opCode = BRLZ; break;
48 case Instruction::SetGT: opCode = BRGZ; break;
49 default:
50 assert(0 && "Unrecognized VM instruction!");
51 opCode = INVALID_OPCODE;
52 break;
53 }
54
55 return opCode;
56}
57
58
59static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000060ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000061 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000062{
63 MachineOpCode opCode = INVALID_OPCODE;
64
65 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
66
67 if (isSigned)
68 {
69 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000070 {
71 case Instruction::SetEQ: opCode = BE; break;
72 case Instruction::SetNE: opCode = BNE; break;
73 case Instruction::SetLE: opCode = BLE; break;
74 case Instruction::SetGE: opCode = BGE; break;
75 case Instruction::SetLT: opCode = BL; break;
76 case Instruction::SetGT: opCode = BG; break;
77 default:
78 assert(0 && "Unrecognized VM instruction!");
79 break;
80 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000081 }
82 else
83 {
84 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000085 {
86 case Instruction::SetEQ: opCode = BE; break;
87 case Instruction::SetNE: opCode = BNE; break;
88 case Instruction::SetLE: opCode = BLEU; break;
89 case Instruction::SetGE: opCode = BCC; break;
90 case Instruction::SetLT: opCode = BCS; break;
91 case Instruction::SetGT: opCode = BGU; break;
92 default:
93 assert(0 && "Unrecognized VM instruction!");
94 break;
95 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000096 }
97
98 return opCode;
99}
100
101static inline MachineOpCode
102ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000103 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000104{
105 MachineOpCode opCode = INVALID_OPCODE;
106
107 switch(setCCInstr->getOpcode())
108 {
109 case Instruction::SetEQ: opCode = FBE; break;
110 case Instruction::SetNE: opCode = FBNE; break;
111 case Instruction::SetLE: opCode = FBLE; break;
112 case Instruction::SetGE: opCode = FBGE; break;
113 case Instruction::SetLT: opCode = FBL; break;
114 case Instruction::SetGT: opCode = FBG; break;
115 default:
116 assert(0 && "Unrecognized VM instruction!");
117 break;
118 }
119
120 return opCode;
121}
122
123
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000124// Create a unique TmpInstruction for a boolean value,
125// representing the CC register used by a branch on that value.
126// For now, hack this using a little static cache of TmpInstructions.
127// Eventually the entire BURG instruction selection should be put
128// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000129// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000130// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000131//
132static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000133GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000134{
Chris Lattner09ff1122002-07-24 21:21:32 +0000135 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000136 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000137 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000138
139 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
140
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000141 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000142 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000143 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000144 boolToTmpCache.clear();
145 }
146
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000147 // Look for tmpI and create a new one otherwise. The new value is
148 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000149 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
150 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000151 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000152
153 return tmpI;
154}
155
156
Chris Lattner20b1ea02001-09-14 03:47:57 +0000157static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000158ChooseBccInstruction(const InstructionNode* instrNode,
159 bool& isFPBranch)
160{
161 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000162 assert(setCCNode->getOpLabel() == SetCCOp);
163 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000164 const Type* setCCType = setCCInstr->getOperand(0)->getType();
165
Vikram S. Adve242a8082002-05-19 15:25:51 +0000166 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
167
168 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000169 return ChooseBFpccInstruction(instrNode, setCCInstr);
170 else
171 return ChooseBpccInstruction(instrNode, setCCInstr);
172}
173
174
175static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000176ChooseMovFpccInstruction(const InstructionNode* instrNode)
177{
178 MachineOpCode opCode = INVALID_OPCODE;
179
180 switch(instrNode->getInstruction()->getOpcode())
181 {
182 case Instruction::SetEQ: opCode = MOVFE; break;
183 case Instruction::SetNE: opCode = MOVFNE; break;
184 case Instruction::SetLE: opCode = MOVFLE; break;
185 case Instruction::SetGE: opCode = MOVFGE; break;
186 case Instruction::SetLT: opCode = MOVFL; break;
187 case Instruction::SetGT: opCode = MOVFG; break;
188 default:
189 assert(0 && "Unrecognized VM instruction!");
190 break;
191 }
192
193 return opCode;
194}
195
196
197// Assumes that SUBcc v1, v2 -> v3 has been executed.
198// In most cases, we want to clear v3 and then follow it by instruction
199// MOVcc 1 -> v3.
200// Set mustClearReg=false if v3 need not be cleared before conditional move.
201// Set valueToMove=0 if we want to conditionally move 0 instead of 1
202// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000203// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000204//
205static MachineOpCode
206ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000207 bool& mustClearReg,
208 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000209{
210 MachineOpCode opCode = INVALID_OPCODE;
211 mustClearReg = true;
212 valueToMove = 1;
213
214 switch(instrNode->getInstruction()->getOpcode())
215 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000216 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000217 case Instruction::SetLE: opCode = MOVLE; break;
218 case Instruction::SetGE: opCode = MOVGE; break;
219 case Instruction::SetLT: opCode = MOVL; break;
220 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000221 case Instruction::SetNE: assert(0 && "No move required!"); break;
222 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000223 }
224
225 return opCode;
226}
227
Chris Lattner20b1ea02001-09-14 03:47:57 +0000228static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000229ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000230{
231 MachineOpCode opCode = INVALID_OPCODE;
232
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000233 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000234 {
235 case ToFloatTy:
236 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000237 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000238 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000239 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000240 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000241 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000242 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000243 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000244 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000245 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000246 break;
247
248 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000249 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
250 // Both functions should treat the integer as a 32-bit value for types
251 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000252 if (opType == Type::SByteTy || opType == Type::UByteTy ||
253 opType == Type::ShortTy || opType == Type::UShortTy ||
254 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000255 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000256 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000257 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000258 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000259 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000260 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000261 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000262 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000263 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000264 break;
265
266 default:
267 break;
268 }
269
270 return opCode;
271}
272
273static inline MachineOpCode
Vikram S. Adve1e606692002-07-31 21:01:34 +0000274ChooseConvertToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000275{
276 MachineOpCode opCode = INVALID_OPCODE;;
277
Vikram S. Adve1e606692002-07-31 21:01:34 +0000278 if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
279 tid==Type::UByteTyID || tid==Type::UShortTyID || tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000280 {
281 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000282 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000283 case Type::FloatTyID: opCode = FSTOI; break;
284 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000285 default:
286 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
287 break;
288 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000289 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000290 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000291 {
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 = FSTOX; break;
295 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000296 default:
297 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
298 break;
299 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000300 }
301 else
302 assert(0 && "Should not get here, Mo!");
303
304 return opCode;
305}
306
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000307MachineInstr*
Vikram S. Adve1e606692002-07-31 21:01:34 +0000308CreateConvertToIntInstr(Type::PrimitiveID destTID, Value* srcVal,Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000309{
Vikram S. Adve1e606692002-07-31 21:01:34 +0000310 MachineOpCode opCode = ChooseConvertToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000311 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
312
313 MachineInstr* M = new MachineInstr(opCode);
314 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
315 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
316 return M;
317}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000318
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000319// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000320// The FP value must be converted to the dest type in an FP register,
321// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000322//
323// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
324// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
325// *only* when converting to an unsigned int. (Unsigned byte, short or long
326// don't have this problem.)
327// For unsigned int, we therefore have to generate the code sequence:
328//
329// if (V > (float) MAXINT) {
330// unsigned result = (unsigned) (V - (float) MAXINT);
331// result = result + (unsigned) MAXINT;
332// }
333// else
334// result = (unsigned int) V;
335//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000336static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000337CreateCodeToConvertFloatToInt(const TargetMachine& target,
338 Value* opVal,
339 Instruction* destI,
340 std::vector<MachineInstr*>& mvec,
341 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000342{
343 // Create a temporary to represent the FP register into which the
344 // int value will placed after conversion. The type of this temporary
345 // depends on the type of FP register to use: single-prec for a 32-bit
346 // int or smaller; double-prec for a 64-bit int.
347 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000348 size_t destSize = target.DataLayout.getTypeSize(destI->getType());
349 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
350 TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000351 mcfi.addTemp(destForCast);
352
353 // Create the fp-to-int conversion code
354 MachineInstr* M = CreateConvertToIntInstr(destI->getType()->getPrimitiveID(),
355 opVal, destForCast);
356 mvec.push_back(M);
357
358 // Create the fpreg-to-intreg copy code
359 target.getInstrInfo().
360 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000361 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000362}
363
364
Chris Lattner20b1ea02001-09-14 03:47:57 +0000365static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000366ChooseAddInstruction(const InstructionNode* instrNode)
367{
368 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
369}
370
371
Chris Lattner20b1ea02001-09-14 03:47:57 +0000372static inline MachineInstr*
373CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000374 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000375{
376 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000377 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000378 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
379 instrNode->leftChild()->getValue());
380 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
381 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000382 return minstr;
383}
384
385static inline MachineInstr*
386CreateAddConstInstruction(const InstructionNode* instrNode)
387{
388 MachineInstr* minstr = NULL;
389
390 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000391 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000392
393 // Cases worth optimizing are:
394 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
395 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
396 //
Chris Lattner9b625032002-05-06 16:15:30 +0000397 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
398 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000399 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000400 minstr = CreateMovFloatInstruction(instrNode,
401 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000402 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000403
404 return minstr;
405}
406
407
408static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000409ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000410{
411 MachineOpCode opCode = INVALID_OPCODE;
412
Chris Lattner0c4e8862002-09-03 01:08:28 +0000413 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000414 {
415 opCode = SUB;
416 }
417 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000418 switch(resultType->getPrimitiveID())
419 {
420 case Type::FloatTyID: opCode = FSUBS; break;
421 case Type::DoubleTyID: opCode = FSUBD; break;
422 default: assert(0 && "Invalid type for SUB instruction"); break;
423 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000424
425 return opCode;
426}
427
428
429static inline MachineInstr*
430CreateSubConstInstruction(const InstructionNode* instrNode)
431{
432 MachineInstr* minstr = NULL;
433
434 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000435 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000436
437 // Cases worth optimizing are:
438 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
439 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
440 //
Chris Lattner9b625032002-05-06 16:15:30 +0000441 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
442 double dval = FPC->getValue();
443 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000444 minstr = CreateMovFloatInstruction(instrNode,
445 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000446 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000447
448 return minstr;
449}
450
451
452static inline MachineOpCode
453ChooseFcmpInstruction(const InstructionNode* instrNode)
454{
455 MachineOpCode opCode = INVALID_OPCODE;
456
457 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
458 switch(operand->getType()->getPrimitiveID()) {
459 case Type::FloatTyID: opCode = FCMPS; break;
460 case Type::DoubleTyID: opCode = FCMPD; break;
461 default: assert(0 && "Invalid type for FCMP instruction"); break;
462 }
463
464 return opCode;
465}
466
467
468// Assumes that leftArg and rightArg are both cast instructions.
469//
470static inline bool
471BothFloatToDouble(const InstructionNode* instrNode)
472{
473 InstrTreeNode* leftArg = instrNode->leftChild();
474 InstrTreeNode* rightArg = instrNode->rightChild();
475 InstrTreeNode* leftArgArg = leftArg->leftChild();
476 InstrTreeNode* rightArgArg = rightArg->leftChild();
477 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
478
479 // Check if both arguments are floats cast to double
480 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000481 leftArgArg->getValue()->getType() == Type::FloatTy &&
482 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000483}
484
485
486static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000487ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000488{
489 MachineOpCode opCode = INVALID_OPCODE;
490
Chris Lattner0c4e8862002-09-03 01:08:28 +0000491 if (resultType->isInteger())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000492 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000493 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000494 switch(resultType->getPrimitiveID())
495 {
496 case Type::FloatTyID: opCode = FMULS; break;
497 case Type::DoubleTyID: opCode = FMULD; break;
498 default: assert(0 && "Invalid type for MUL instruction"); break;
499 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000500
501 return opCode;
502}
503
504
Vikram S. Adve510eec72001-11-04 21:59:14 +0000505
Chris Lattner20b1ea02001-09-14 03:47:57 +0000506static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000507CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000508 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000509{
510 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000511 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
512 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
513 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000514 return minstr;
515}
516
517
Vikram S. Adve242a8082002-05-19 15:25:51 +0000518// Create instruction sequence for any shift operation.
519// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
520// requires a second instruction for explicit sign-extension.
521// Note that we only have to worry about a sign-bit appearing in the
522// most significant bit of the operand after shifting (e.g., bit 32 of
523// Int or bit 16 of Short), so we do not have to worry about results
524// that are as large as a normal integer register.
525//
526static inline void
527CreateShiftInstructions(const TargetMachine& target,
528 Function* F,
529 MachineOpCode shiftOpCode,
530 Value* argVal1,
531 Value* optArgVal2, /* Use optArgVal2 if not NULL */
532 unsigned int optShiftNum, /* else use optShiftNum */
533 Instruction* destVal,
534 vector<MachineInstr*>& mvec,
535 MachineCodeForInstruction& mcfi)
536{
537 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
538 "Large shift sizes unexpected, but can be handled below: "
539 "You need to check whether or not it fits in immed field below");
540
541 // If this is a logical left shift of a type smaller than the standard
542 // integer reg. size, we have to extend the sign-bit into upper bits
543 // of dest, so we need to put the result of the SLL into a temporary.
544 //
545 Value* shiftDest = destVal;
546 const Type* opType = argVal1->getType();
547 unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
548 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
549 && opSize < target.DataLayout.getIntegerRegize())
550 { // put SLL result into a temporary
551 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
552 mcfi.addTemp(shiftDest);
553 }
554
555 MachineInstr* M = (optArgVal2 != NULL)
556 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
557 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
558 mvec.push_back(M);
559
560 if (shiftDest != destVal)
561 { // extend the sign-bit of the result into all upper bits of dest
562 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
563 target.getInstrInfo().
564 CreateSignExtensionInstructions(target, F, shiftDest, 8*opSize,
565 destVal, mvec, mcfi);
566 }
567}
568
569
Vikram S. Adve74825322002-03-18 03:15:35 +0000570// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000571// create a cheaper instruction.
572// This returns the approximate cost of the instructions generated,
573// which is used to pick the cheapest when both operands are constant.
574static inline unsigned int
Vikram S. Adve242a8082002-05-19 15:25:51 +0000575CreateMulConstInstruction(const TargetMachine &target, Function* F,
576 Value* lval, Value* rval, Instruction* destVal,
577 vector<MachineInstr*>& mvec,
578 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000579{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000580 /* Use max. multiply cost, viz., cost of MULX */
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000581 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000582 unsigned int firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000583
584 Value* constOp = rval;
585 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000586 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000587
588 // Cases worth optimizing are:
589 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
590 // (2) Multiply by 2^x for integer types: replace with Shift
591 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000592 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000593
Chris Lattner0c4e8862002-09-03 01:08:28 +0000594 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000595 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000596 bool isValidConst;
597 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
598 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000599 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000600 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000601 bool needNeg = false;
602 if (C < 0)
603 {
604 needNeg = true;
605 C = -C;
606 }
607
608 if (C == 0 || C == 1)
609 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000610 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000611 MachineInstr* M = (C == 0)
612 ? Create3OperandInstr_Reg(ADD,
613 target.getRegInfo().getZeroRegNum(),
614 target.getRegInfo().getZeroRegNum(),
615 destVal)
616 : Create3OperandInstr_Reg(ADD, lval,
617 target.getRegInfo().getZeroRegNum(),
618 destVal);
619 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000620 }
Chris Lattner36346c72002-05-19 21:20:19 +0000621 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000622 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000623 unsigned int opSize = target.DataLayout.getTypeSize(resultType);
624 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
625 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
626 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000627 }
628
Vikram S. Adve242a8082002-05-19 15:25:51 +0000629 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000630 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000631 MachineInstr* M = CreateIntNegInstruction(target, destVal);
632 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000633 }
634 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000635 }
636 else
637 {
Chris Lattner9b625032002-05-06 16:15:30 +0000638 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000639 {
Chris Lattner9b625032002-05-06 16:15:30 +0000640 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000641 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000642 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000643 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000644 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
645 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000646 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
647 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000648 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000649 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000650 }
651
Vikram S. Adve242a8082002-05-19 15:25:51 +0000652 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000653 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000654 cost = 0;
655 for (unsigned int i=firstNewInstr; i < mvec.size(); ++i)
656 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000657 }
658
659 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000660}
661
662
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000663// Does not create any instructions if we cannot exploit constant to
664// create a cheaper instruction.
665//
666static inline void
667CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000668 Function* F,
669 Value* lval, Value* rval,
670 Instruction* destVal,
671 vector<MachineInstr*>& mvec,
672 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000673{
674 Value* constOp;
675 if (isa<Constant>(lval) && isa<Constant>(rval))
676 { // both operands are constant: try both orders!
677 vector<MachineInstr*> mvec1, mvec2;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000678 unsigned int lcost = CreateMulConstInstruction(target, F, lval, rval,
679 destVal, mvec1, mcfi);
680 unsigned int rcost = CreateMulConstInstruction(target, F, rval, lval,
681 destVal, mvec2, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000682 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
683 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
684 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
685
686 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
687 delete maxcostMvec[i];
688 }
689 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000690 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000691 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000692 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000693
694 // else neither is constant
695 return;
696}
697
Vikram S. Adve74825322002-03-18 03:15:35 +0000698// Return NULL if we cannot exploit constant to create a cheaper instruction
699static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000700CreateMulInstruction(const TargetMachine &target, Function* F,
701 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000702 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000703 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000704 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
705{
706 unsigned int L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000707 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000708 if (mvec.size() == L)
709 { // no instructions were added so create MUL reg, reg, reg.
710 // Use FSMULD if both operands are actually floats cast to doubles.
711 // Otherwise, use the default opcode for the appropriate type.
712 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
713 ? forceMulOp
714 : ChooseMulInstructionByType(destVal->getType()));
715 MachineInstr* M = new MachineInstr(mulOp);
716 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
717 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
718 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
719 mvec.push_back(M);
720 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000721}
722
723
Vikram S. Adve510eec72001-11-04 21:59:14 +0000724// Generate a divide instruction for Div or Rem.
725// For Rem, this assumes that the operand type will be signed if the result
726// type is signed. This is correct because they must have the same sign.
727//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000728static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000729ChooseDivInstruction(TargetMachine &target,
730 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000731{
732 MachineOpCode opCode = INVALID_OPCODE;
733
734 const Type* resultType = instrNode->getInstruction()->getType();
735
Chris Lattner0c4e8862002-09-03 01:08:28 +0000736 if (resultType->isInteger())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000737 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000738 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000739 switch(resultType->getPrimitiveID())
740 {
741 case Type::FloatTyID: opCode = FDIVS; break;
742 case Type::DoubleTyID: opCode = FDIVD; break;
743 default: assert(0 && "Invalid type for DIV instruction"); break;
744 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000745
746 return opCode;
747}
748
749
Vikram S. Adve74825322002-03-18 03:15:35 +0000750// Return NULL if we cannot exploit constant to create a cheaper instruction
751static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000752CreateDivConstInstruction(TargetMachine &target,
753 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000754 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000755{
Vikram S. Adve74825322002-03-18 03:15:35 +0000756 MachineInstr* minstr1 = NULL;
757 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000758
759 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000760 if (! isa<Constant>(constOp))
761 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000762
763 // Cases worth optimizing are:
764 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
765 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
766 //
767 const Type* resultType = instrNode->getInstruction()->getType();
768
Chris Lattner0c4e8862002-09-03 01:08:28 +0000769 if (resultType->isInteger())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000770 {
771 unsigned pow;
772 bool isValidConst;
773 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
774 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000775 {
776 bool needNeg = false;
777 if (C < 0)
778 {
779 needNeg = true;
780 C = -C;
781 }
782
783 if (C == 1)
784 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000785 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000786 minstr1->SetMachineOperandVal(0,
787 MachineOperand::MO_VirtualRegister,
788 instrNode->leftChild()->getValue());
789 minstr1->SetMachineOperandReg(1,
790 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000791 }
Chris Lattner36346c72002-05-19 21:20:19 +0000792 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000793 {
794 MachineOpCode opCode= ((resultType->isSigned())
795 ? (resultType==Type::LongTy)? SRAX : SRA
796 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000797 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000798 minstr1->SetMachineOperandVal(0,
799 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000800 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000801 minstr1->SetMachineOperandConst(1,
802 MachineOperand::MO_UnextendedImmed,
803 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000804 }
805
Vikram S. Adve74825322002-03-18 03:15:35 +0000806 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000807 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000808 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000809 instrNode->getValue());
810 }
811 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000812 }
813 else
814 {
Chris Lattner9b625032002-05-06 16:15:30 +0000815 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000816 {
Chris Lattner9b625032002-05-06 16:15:30 +0000817 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000818 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000819 {
820 bool needNeg = (dval < 0);
821
822 MachineOpCode opCode = needNeg
823 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
824 : (resultType == Type::FloatTy? FMOVS : FMOVD);
825
Vikram S. Adve74825322002-03-18 03:15:35 +0000826 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000827 minstr1->SetMachineOperandVal(0,
828 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000829 instrNode->leftChild()->getValue());
830 }
831 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000832 }
833
Vikram S. Adve74825322002-03-18 03:15:35 +0000834 if (minstr1 != NULL)
835 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
836 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000837
Vikram S. Adve74825322002-03-18 03:15:35 +0000838 if (minstr1)
839 mvec.push_back(minstr1);
840 if (minstr2)
841 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000842}
843
844
Vikram S. Adve74825322002-03-18 03:15:35 +0000845static void
846CreateCodeForVariableSizeAlloca(const TargetMachine& target,
847 Instruction* result,
848 unsigned int tsize,
849 Value* numElementsVal,
850 vector<MachineInstr*>& getMvec)
851{
852 MachineInstr* M;
853
854 // Create a Value to hold the (constant) element size
855 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
856
857 // Get the constant offset from SP for dynamically allocated storage
858 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000859 assert(result && result->getParent() && "Result value is not part of a fn?");
860 Function *F = result->getParent()->getParent();
861 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000862 bool growUp;
863 ConstantSInt* dynamicAreaOffset =
864 ConstantSInt::get(Type::IntTy,
865 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
866 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
867
868 // Create a temporary value to hold the result of MUL
869 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
870 MachineCodeForInstruction::get(result).addTemp(tmpProd);
871
872 // Instruction 1: mul numElements, typeSize -> tmpProd
873 M = new MachineInstr(MULX);
874 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
875 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
876 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
877 getMvec.push_back(M);
878
879 // Instruction 2: sub %sp, tmpProd -> %sp
880 M = new MachineInstr(SUB);
881 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
882 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
883 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
884 getMvec.push_back(M);
885
886 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
887 M = new MachineInstr(ADD);
888 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
889 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
890 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
891 getMvec.push_back(M);
892}
893
894
895static void
896CreateCodeForFixedSizeAlloca(const TargetMachine& target,
897 Instruction* result,
898 unsigned int tsize,
899 unsigned int numElements,
900 vector<MachineInstr*>& getMvec)
901{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000902 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000903 "Result value is not part of a function?");
904 Function *F = result->getParent()->getParent();
905 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000906
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000907 // Check if the offset would small enough to use as an immediate in
908 // load/stores (check LDX because all load/stores have the same-size immediate
909 // field). If not, put the variable in the dynamically sized area of the
910 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000911 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000912 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000913 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000914 tsize * numElements);
915 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
916 {
917 CreateCodeForVariableSizeAlloca(target, result, tsize,
918 ConstantSInt::get(Type::IntTy,numElements),
919 getMvec);
920 return;
921 }
922
923 // else offset fits in immediate field so go ahead and allocate it.
924 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
925
926 // Create a temporary Value to hold the constant offset.
927 // This is needed because it may not fit in the immediate field.
928 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
929
930 // Instruction 1: add %fp, offsetFromFP -> result
931 MachineInstr* M = new MachineInstr(ADD);
932 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
933 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
934 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
935
936 getMvec.push_back(M);
937}
938
939
940
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000941// Check for a constant (uint) 0.
942inline bool
943IsZero(Value* idx)
944{
945 return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
946}
Vikram S. Adve242a8082002-05-19 15:25:51 +0000947
948
Chris Lattner20b1ea02001-09-14 03:47:57 +0000949//------------------------------------------------------------------------
950// Function SetOperandsForMemInstr
951//
952// Choose addressing mode for the given load or store instruction.
953// Use [reg+reg] if it is an indexed reference, and the index offset is
954// not a constant or if it cannot fit in the offset field.
955// Use [reg+offset] in all other cases.
956//
957// This assumes that all array refs are "lowered" to one of these forms:
958// %x = load (subarray*) ptr, constant ; single constant offset
959// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
960// Generally, this should happen via strength reduction + LICM.
961// Also, strength reduction should take care of using the same register for
962// the loop index variable and an array index, when that is profitable.
963//------------------------------------------------------------------------
964
965static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000966SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000967 const InstructionNode* vmInstrNode,
968 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000969{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000970 Instruction* memInst = vmInstrNode->getInstruction();
971 vector<MachineInstr*>::iterator mvecI = mvec.end() - 1;
972
973 // Index vector, ptr value, and flag if all indices are const.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000974 vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000975 bool allConstantIndices;
976 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000977
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000978 // Now create the appropriate operands for the machine instruction.
979 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000980 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000981 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000982 MachineOperand::MachineOperandType offsetOpType =
983 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000984
Vikram S. Adve74825322002-03-18 03:15:35 +0000985 // Check if there is an index vector and if so, compute the
986 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000987 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000988 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000989 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000990 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000991
Vikram S. Adve242a8082002-05-19 15:25:51 +0000992 // If all indices are constant, compute the combined offset directly.
993 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000994 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000995 // Compute the offset value using the index vector. Create a
996 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000997 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
998 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000999 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001000 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001001 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001002 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001003 // be an array ref, and must have been lowered to a single non-zero
1004 // offset. (An extra leading zero offset, if any, can be ignored.)
1005 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001006 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001007 bool firstIdxIsZero = IsZero(idxVec[0]);
1008 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001009 && "Array refs must be lowered before Instruction Selection");
1010
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001011 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001012 assert(! isa<Constant>(idxVal) && "Need to sign-extend uint to 64b!");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001013
1014 vector<MachineInstr*> mulVec;
1015 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1016 MachineCodeForInstruction::get(memInst).addTemp(addr);
1017
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001018 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001019 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001020 const Type* vecType = (firstIdxIsZero
1021 ? GetElementPtrInst::getIndexedType(ptrType,
1022 std::vector<Value*>(1U, idxVec[0]),
1023 /*AllowCompositeLeaf*/ true)
1024 : ptrType);
1025 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
1026 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::UIntTy,
1027 target.DataLayout.getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001028
1029 // CreateMulInstruction() folds constants intelligently enough.
1030 CreateMulInstruction(target,
1031 memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001032 idxVal, /* lval, not likely to be const*/
1033 eltSizeVal, /* rval, likely to be constant */
1034 addr, /* result */
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001035 mulVec,
1036 MachineCodeForInstruction::get(memInst),
1037 INVALID_MACHINE_OPCODE);
1038
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001039 // Sign-extend the result of MUL from 32 to 64 bits.
1040 target.getInstrInfo().CreateSignExtensionInstructions(target, memInst->getParent()->getParent(), addr, /*srcSizeInBits*/32, addr, mulVec, MachineCodeForInstruction::get(memInst));
1041
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001042 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1043 // to point to the same instruction it pointed to before.
1044 assert(mulVec.size() > 0 && "No multiply code created?");
1045 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1046 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1047 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1048
1049 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001050 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001051 }
1052 else
1053 {
1054 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1055 smallConstOffset = 0;
1056 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001057
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001058 // For STORE:
1059 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1060 // For LOAD or GET_ELEMENT_PTR,
1061 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1062 //
1063 unsigned offsetOpNum, ptrOpNum;
1064 if (memInst->getOpcode() == Instruction::Store)
1065 {
1066 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1067 vmInstrNode->leftChild()->getValue());
1068 ptrOpNum = 1;
1069 offsetOpNum = 2;
1070 }
1071 else
1072 {
1073 ptrOpNum = 0;
1074 offsetOpNum = 1;
1075 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1076 memInst);
1077 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001078
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001079 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1080 ptrVal);
1081
Chris Lattner20b1ea02001-09-14 03:47:57 +00001082 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1083 {
1084 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001085 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1086 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001087 }
1088 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001089 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1090 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001091}
1092
1093
Chris Lattner20b1ea02001-09-14 03:47:57 +00001094//
1095// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001096// in place of the use(s) of that instruction in node `parent'.
1097// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001098// Also make sure to skip over a parent who:
1099// (1) is a list node in the Burg tree, or
1100// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001101//
1102static void
1103ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001104 InstrTreeNode* parent,
1105 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001106{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001107 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1108
Chris Lattner20b1ea02001-09-14 03:47:57 +00001109 Instruction* unusedOp = treeNode->getInstruction();
1110 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001111
1112 // The parent itself may be a list node, so find the real parent instruction
1113 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1114 {
1115 parent = parent->parent();
1116 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1117 }
1118 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1119
1120 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001121 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001122
1123 // The parent's mvec would be empty if it was itself forwarded.
1124 // Recursively call ForwardOperand in that case...
1125 //
1126 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001127 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001128 assert(parent->parent() != NULL &&
1129 "Parent could not have been forwarded, yet has no instructions?");
1130 ForwardOperand(treeNode, parent->parent(), operandNum);
1131 }
1132 else
1133 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001134 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001135 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001136 MachineInstr* minstr = mvec[i];
1137 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001138 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001139 const MachineOperand& mop = minstr->getOperand(i);
1140 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1141 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001142 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001143 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001144 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001145
1146 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1147 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001148 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001149 minstr->implicitRefIsDefined(i),
1150 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001151 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001152 }
1153}
1154
1155
Vikram S. Adve242a8082002-05-19 15:25:51 +00001156inline bool
1157AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001158{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001159 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1160 UI != UE; ++UI)
1161 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1162 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1163 return false;
1164 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001165}
1166
Vikram S. Advefb361122001-10-22 13:36:31 +00001167//******************* Externally Visible Functions *************************/
1168
Vikram S. Advefb361122001-10-22 13:36:31 +00001169//------------------------------------------------------------------------
1170// External Function: ThisIsAChainRule
1171//
1172// Purpose:
1173// Check if a given BURG rule is a chain rule.
1174//------------------------------------------------------------------------
1175
1176extern bool
1177ThisIsAChainRule(int eruleno)
1178{
1179 switch(eruleno)
1180 {
1181 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001182 case 123:
1183 case 124:
1184 case 125:
1185 case 126:
1186 case 127:
1187 case 128:
1188 case 129:
1189 case 130:
1190 case 131:
1191 case 132:
1192 case 133:
1193 case 155:
1194 case 221:
1195 case 222:
1196 case 241:
1197 case 242:
1198 case 243:
1199 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001200 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001201 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001202 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001203
Vikram S. Advefb361122001-10-22 13:36:31 +00001204 default:
1205 return false; break;
1206 }
1207}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001208
1209
1210//------------------------------------------------------------------------
1211// External Function: GetInstructionsByRule
1212//
1213// Purpose:
1214// Choose machine instructions for the SPARC according to the
1215// patterns chosen by the BURG-generated parser.
1216//------------------------------------------------------------------------
1217
Vikram S. Adve74825322002-03-18 03:15:35 +00001218void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001219GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001220 int ruleForNode,
1221 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001222 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001223 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001224{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001225 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001226 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001227 int nextRule;
1228 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001229 unsigned int allocaSize = 0;
1230 MachineInstr* M, *M2;
1231 unsigned int L;
1232
1233 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001234
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001235 // If the code for this instruction was folded into the parent (user),
1236 // then do nothing!
1237 if (subtreeRoot->isFoldedIntoParent())
1238 return;
1239
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001240 //
1241 // Let's check for chain rules outside the switch so that we don't have
1242 // to duplicate the list of chain rule production numbers here again
1243 //
1244 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001245 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001246 // Chain rules have a single nonterminal on the RHS.
1247 // Get the rule that matches the RHS non-terminal and use that instead.
1248 //
1249 assert(nts[0] && ! nts[1]
1250 && "A chain rule should have only one RHS non-terminal!");
1251 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1252 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001253 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001254 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001255 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001256 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001257 switch(ruleForNode) {
1258 case 1: // stmt: Ret
1259 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001260 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001261 // for moving return value to appropriate register.
1262 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001263 // Mark the return value register as an implicit ref of
1264 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001265 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001266 ReturnInst *returnInstr =
1267 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001268 assert(returnInstr->getOpcode() == Instruction::Ret);
1269
Chris Lattner9c461082002-02-03 07:50:56 +00001270 Instruction* returnReg = new TmpInstruction(returnInstr);
1271 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001272
Vikram S. Adve74825322002-03-18 03:15:35 +00001273 M = new MachineInstr(JMPLRET);
1274 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001275 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001276 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001277 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001278 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001279
Vikram S. Advea995e602001-10-11 04:23:19 +00001280 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001281 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001282
Vikram S. Adve74825322002-03-18 03:15:35 +00001283 mvec.push_back(M);
1284 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001285
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001286 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001287 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001288
1289 case 3: // stmt: Store(reg,reg)
1290 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001291 mvec.push_back(new MachineInstr(
1292 ChooseStoreInstruction(
1293 subtreeRoot->leftChild()->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001294 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001295 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001296
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001297 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001298 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001299 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001300 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001301 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001302
1303 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001304 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001305 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001306
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001307 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001308 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001309 // If the constant is ZERO, we can use the branch-on-integer-register
1310 // instructions and avoid the SUBcc instruction entirely.
1311 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001312 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001313 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1314 assert(constNode &&
1315 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001316 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001317 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001318
Chris Lattner0c4e8862002-09-03 01:08:28 +00001319 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001320 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001321 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1322 && isValidConst)
1323 {
1324 // That constant is a zero after all...
1325 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001326 // Mark the setCC node so that no code is generated for it.
1327 InstructionNode* setCCNode = (InstructionNode*)
1328 subtreeRoot->leftChild();
1329 assert(setCCNode->getOpLabel() == SetCCOp);
1330 setCCNode->markFoldedIntoParent();
1331
1332 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1333
Vikram S. Adve74825322002-03-18 03:15:35 +00001334 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1335 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001336 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001337 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1338 brInst->getSuccessor(0));
1339 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001340
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001341 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001342 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001343
1344 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001345 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001346 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001347 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001348 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001349
1350 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001351 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001352
1353 break;
1354 }
1355 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001356 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001357
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001358 case 6: // stmt: BrCond(setCC)
1359 { // bool => boolean was computed with SetCC.
1360 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001361 // If it is an integer CC, we also need to find the unique
1362 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001363 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001364 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001365 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001366 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001367
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001368 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1369 brInst->getParent()->getParent(),
1370 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001371
Vikram S. Adve74825322002-03-18 03:15:35 +00001372 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1373 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1374 brInst->getSuccessor(0));
1375 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001376
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001377 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001378 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001379
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001380 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001381 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001382 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001383 brInst->getSuccessor(1));
1384 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001385
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001386 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001387 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001388 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001389 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001390
1391 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001392 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001393 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001394 Constant* constVal =
1395 cast<Constant>(subtreeRoot->leftChild()->getValue());
1396 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001397
Vikram S. Adve74825322002-03-18 03:15:35 +00001398 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001399 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001400 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001401 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001402
1403 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001404 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001405 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001406 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001407
1408 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001409 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001410 // Just use the branch-on-integer-register instruction!
1411 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001412 M = new MachineInstr(BRNZ);
1413 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001414 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001415 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001416 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001417 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001418
1419 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001420 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001421
1422 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001423 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001424 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001425 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001426 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001427
1428 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001429 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001430 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001431 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001432
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001433 case 9: // stmt: Switch(reg)
1434 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001435 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001436
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001437 case 10: // reg: VRegList(reg, reg)
1438 assert(0 && "VRegList should never be the topmost non-chain rule");
1439 break;
1440
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001441 case 21: // bool: Not(bool,reg): Both these are implemented as:
1442 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1443 { // First find the unary operand. It may be left or right, usually right.
1444 Value* notArg = BinaryOperator::getNotArgument(
1445 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1446 mvec.push_back(Create3OperandInstr_Reg(XNOR, notArg,
1447 target.getRegInfo().getZeroRegNum(),
1448 subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001449 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001450 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001451
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001452 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001453 {
1454 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001455 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001456 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001457 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001458 }
1459
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001460 case 23: // reg: ToUByteTy(reg)
1461 case 25: // reg: ToUShortTy(reg)
1462 case 27: // reg: ToUIntTy(reg)
1463 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001464 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001465 Instruction* destI = subtreeRoot->getInstruction();
1466 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001467 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001468 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001469 {
1470 unsigned opSize = target.DataLayout.getTypeSize(opType);
1471 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
1472 if (opSize > destSize ||
1473 (opType->isSigned()
1474 && destSize < target.DataLayout.getIntegerRegize()))
1475 { // operand is larger than dest,
1476 // OR both are equal but smaller than the full register size
1477 // AND operand is signed, so it may have extra sign bits:
1478 // mask high bits using AND
1479 M = Create3OperandInstr(AND, opVal,
1480 ConstantUInt::get(Type::ULongTy,
1481 ((uint64_t) 1 << 8*destSize) - 1),
1482 destI);
1483 mvec.push_back(M);
1484 }
1485 else
1486 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001487 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001488 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001489 {
1490 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1491 MachineCodeForInstruction::get(destI));
1492 maskUnsignedResult = true; // not handled by convert code
1493 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001494 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001495 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1496
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001497 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001498 }
1499
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001500 case 24: // reg: ToSByteTy(reg)
1501 case 26: // reg: ToShortTy(reg)
1502 case 28: // reg: ToIntTy(reg)
1503 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001504 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001505 Instruction* destI = subtreeRoot->getInstruction();
1506 Value* opVal = subtreeRoot->leftChild()->getValue();
1507 MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001508
Vikram S. Adve242a8082002-05-19 15:25:51 +00001509 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001510 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001511 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001512 // These operand types have the same format as the destination,
1513 // but may have different size: add sign bits or mask as needed.
1514 //
1515 const Type* destType = destI->getType();
1516 unsigned opSize = target.DataLayout.getTypeSize(opType);
1517 unsigned destSize = target.DataLayout.getTypeSize(destType);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001518
1519 if (opSize < destSize ||
1520 (opSize == destSize &&
1521 opSize == target.DataLayout.getIntegerRegize()))
1522 { // operand is smaller or both operand and result fill register
1523 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001524 }
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001525 else
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001526 { // need to mask (possibly) and then sign-extend (definitely)
1527 Value* srcForSignExt = opVal;
1528 unsigned srcSizeForSignExt = 8 * opSize;
1529 if (opSize > destSize)
1530 { // operand is larger than dest: mask high bits
1531 TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
1532 destI, "maskHi");
1533 mcfi.addTemp(tmpI);
1534 M = Create3OperandInstr(AND, opVal,
1535 ConstantUInt::get(Type::ULongTy,
1536 ((uint64_t) 1 << 8*destSize)-1),
1537 tmpI);
1538 mvec.push_back(M);
1539 srcForSignExt = tmpI;
1540 srcSizeForSignExt = 8 * destSize;
1541 }
1542
1543 // sign-extend
1544 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), srcForSignExt, srcSizeForSignExt, destI, mvec, mcfi);
1545 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001546 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001547 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001548 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001549 else
1550 assert(0 && "Unrecognized operand type for convert-to-signed");
1551
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001552 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001553 }
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001554
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001555 case 31: // reg: ToFloatTy(reg):
1556 case 32: // reg: ToDoubleTy(reg):
1557 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001558
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001559 // If this instruction has a parent (a user) in the tree
1560 // and the user is translated as an FsMULd instruction,
1561 // then the cast is unnecessary. So check that first.
1562 // In the future, we'll want to do the same for the FdMULq instruction,
1563 // so do the check here instead of only for ToFloatTy(reg).
1564 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001565 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001566 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001567 const MachineCodeForInstruction& mcfi =
1568 MachineCodeForInstruction::get(
1569 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1570 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1571 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001572 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001573
1574 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001575 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001576 Value* leftVal = subtreeRoot->leftChild()->getValue();
1577 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001578 MachineOpCode opCode=ChooseConvertToFloatInstr(
1579 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001580 if (opCode == INVALID_OPCODE) // no conversion needed
1581 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001582 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001583 }
1584 else
1585 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001586 // If the source operand is a non-FP type it must be
1587 // first copied from int to float register via memory!
1588 Instruction *dest = subtreeRoot->getInstruction();
1589 Value* srcForCast;
1590 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001591 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001592 {
1593 // Create a temporary to represent the FP register
1594 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001595 // The type of this temporary will determine the FP
1596 // register used: single-prec for a 32-bit int or smaller,
1597 // double-prec for a 64-bit int.
1598 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001599 uint64_t srcSize =
1600 target.DataLayout.getTypeSize(leftVal->getType());
1601 Type* tmpTypeToUse =
1602 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1603 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001604 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001605 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001606 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001607
Vikram S. Adve242a8082002-05-19 15:25:51 +00001608 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001609 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001610 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001611 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001612 }
1613 else
1614 srcForCast = leftVal;
1615
Vikram S. Adve74825322002-03-18 03:15:35 +00001616 M = new MachineInstr(opCode);
1617 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1618 srcForCast);
1619 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1620 dest);
1621 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001622 }
1623 }
1624 break;
1625
1626 case 19: // reg: ToArrayTy(reg):
1627 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001628 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001629 break;
1630
1631 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001632 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001633 M = CreateAddConstInstruction(subtreeRoot);
1634 if (M != NULL)
1635 {
1636 mvec.push_back(M);
1637 break;
1638 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001639 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001640
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001641 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001642 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001643 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1644 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001645 break;
1646
1647 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001648 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001649 M = CreateSubConstInstruction(subtreeRoot);
1650 if (M != NULL)
1651 {
1652 mvec.push_back(M);
1653 break;
1654 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001655 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001656
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001657 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001658 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001659 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1660 subtreeRoot->getInstruction()->getType())));
1661 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001662 break;
1663
1664 case 135: // reg: Mul(todouble, todouble)
1665 checkCast = true;
1666 // FALL THROUGH
1667
1668 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001669 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001670 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001671 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1672 ? FSMULD
1673 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001674 Instruction* mulInstr = subtreeRoot->getInstruction();
1675 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001676 subtreeRoot->leftChild()->getValue(),
1677 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001678 mulInstr, mvec,
1679 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001680 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001681 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001682 case 335: // reg: Mul(todouble, todoubleConst)
1683 checkCast = true;
1684 // FALL THROUGH
1685
1686 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001687 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001688 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001689 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1690 ? FSMULD
1691 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001692 Instruction* mulInstr = subtreeRoot->getInstruction();
1693 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001694 subtreeRoot->leftChild()->getValue(),
1695 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001696 mulInstr, mvec,
1697 MachineCodeForInstruction::get(mulInstr),
1698 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001699 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001700 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001701 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001702 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001703 L = mvec.size();
1704 CreateDivConstInstruction(target, subtreeRoot, mvec);
1705 if (mvec.size() > L)
1706 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001707 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001708
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001709 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001710 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001711 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1712 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001713 break;
1714
1715 case 37: // reg: Rem(reg, reg)
1716 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001717 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001718 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001719 Instruction* remInstr = subtreeRoot->getInstruction();
1720
Chris Lattner9c461082002-02-03 07:50:56 +00001721 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001722 subtreeRoot->leftChild()->getValue(),
1723 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001724 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001725 quot,
1726 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001727 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001728
Vikram S. Adve74825322002-03-18 03:15:35 +00001729 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1730 Set3OperandsFromInstr(M, subtreeRoot, target);
1731 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1732 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001733
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001734 M = Create3OperandInstr(ChooseMulInstructionByType(
1735 subtreeRoot->getInstruction()->getType()),
1736 quot, subtreeRoot->rightChild()->getValue(),
1737 prod);
Vikram S. Adve74825322002-03-18 03:15:35 +00001738 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001739
Vikram S. Adve74825322002-03-18 03:15:35 +00001740 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001741 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001742 Set3OperandsFromInstr(M, subtreeRoot, target);
1743 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1744 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001745
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001746 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001747 }
1748
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001749 case 38: // bool: And(bool, bool)
1750 case 238: // bool: And(bool, boolconst)
1751 case 338: // reg : BAnd(reg, reg)
1752 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001753 mvec.push_back(new MachineInstr(AND));
1754 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001755 break;
1756
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001757 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001758 case 438: // bool: BAnd(bool, bnot)
1759 { // Use the argument of NOT as the second argument!
1760 // Mark the NOT node so that no code is generated for it.
1761 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1762 Value* notArg = BinaryOperator::getNotArgument(
1763 cast<BinaryOperator>(notNode->getInstruction()));
1764 notNode->markFoldedIntoParent();
1765 mvec.push_back(Create3OperandInstr(ANDN,
1766 subtreeRoot->leftChild()->getValue(),
1767 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001768 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001769 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001770
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001771 case 39: // bool: Or(bool, bool)
1772 case 239: // bool: Or(bool, boolconst)
1773 case 339: // reg : BOr(reg, reg)
1774 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001775 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001776 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001777 break;
1778
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001779 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001780 case 439: // bool: BOr(bool, bnot)
1781 { // Use the argument of NOT as the second argument!
1782 // Mark the NOT node so that no code is generated for it.
1783 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1784 Value* notArg = BinaryOperator::getNotArgument(
1785 cast<BinaryOperator>(notNode->getInstruction()));
1786 notNode->markFoldedIntoParent();
1787 mvec.push_back(Create3OperandInstr(ORN,
1788 subtreeRoot->leftChild()->getValue(),
1789 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001790 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001791 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001792
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001793 case 40: // bool: Xor(bool, bool)
1794 case 240: // bool: Xor(bool, boolconst)
1795 case 340: // reg : BXor(reg, reg)
1796 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001797 mvec.push_back(new MachineInstr(XOR));
1798 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001799 break;
1800
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001801 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001802 case 440: // bool: BXor(bool, bnot)
1803 { // Use the argument of NOT as the second argument!
1804 // Mark the NOT node so that no code is generated for it.
1805 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1806 Value* notArg = BinaryOperator::getNotArgument(
1807 cast<BinaryOperator>(notNode->getInstruction()));
1808 notNode->markFoldedIntoParent();
1809 mvec.push_back(Create3OperandInstr(XNOR,
1810 subtreeRoot->leftChild()->getValue(),
1811 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001812 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001813 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001814
1815 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001816 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001817 // If the SetCC was folded into the user (parent), it will be
1818 // caught above. All other cases are the same as case 42,
1819 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001820 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001821 case 42: // bool: SetCC(reg, reg):
1822 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001823 // This generates a SUBCC instruction, putting the difference in
1824 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001825 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001826 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001827 // than a branch instruction, or if it is used outside the current
1828 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001829 // computed and stored in the result register. Otherwise, discard
1830 // the difference (by using %g0) and keep only the condition code.
1831 //
1832 // To compute the boolean result in a register we use a conditional
1833 // move, unless the result of the SUBCC instruction can be used as
1834 // the bool! This assumes that zero is FALSE and any non-zero
1835 // integer is TRUE.
1836 //
1837 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1838 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001839
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001840 bool keepBoolVal = parentNode == NULL ||
1841 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001842 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001843 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1844 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1845
1846 bool mustClearReg;
1847 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001848 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001849
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001850 // Mark the 4th operand as being a CC register, and as a def
1851 // A TmpInstruction is created to represent the CC "result".
1852 // Unlike other instances of TmpInstruction, this one is used
1853 // by machine code of multiple LLVM instructions, viz.,
1854 // the SetCC and the branch. Make sure to get the same one!
1855 // Note that we do this even for FP CC registers even though they
1856 // are explicit operands, because the type of the operand
1857 // needs to be a floating point condition code, not an integer
1858 // condition code. Think of this as casting the bool result to
1859 // a FP condition code register.
1860 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001861 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001862 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001863
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001864 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1865 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001866 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001867 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001868
1869 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001870 {
1871 // Integer condition: dest. should be %g0 or an integer register.
1872 // If result must be saved but condition is not SetEQ then we need
1873 // a separate instruction to compute the bool result, so discard
1874 // result of SUBcc instruction anyway.
1875 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001876 M = new MachineInstr(SUBcc);
1877 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1878 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1879 tmpForCC, /*def*/true);
1880 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001881
1882 if (computeBoolVal)
1883 { // recompute bool using the integer condition codes
1884 movOpCode =
1885 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1886 }
1887 }
1888 else
1889 {
1890 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001891 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1892 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001893 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001894 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001895 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001896 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001897 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001898 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001899
1900 if (computeBoolVal)
1901 {// recompute bool using the FP condition codes
1902 mustClearReg = true;
1903 valueToMove = 1;
1904 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1905 }
1906 }
1907
1908 if (computeBoolVal)
1909 {
1910 if (mustClearReg)
1911 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001912 M = new MachineInstr(SETHI);
1913 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1914 (int64_t)0);
1915 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1916 setCCInstr);
1917 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001918 }
1919
1920 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001921 // Mark the register as a use (as well as a def) because the old
1922 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001923 M = new MachineInstr(movOpCode);
1924 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1925 tmpForCC);
1926 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1927 valueToMove);
1928 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001929 setCCInstr, /*isDef*/ true,
1930 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001931 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001932 }
1933 break;
1934 }
1935
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001936 case 51: // reg: Load(reg)
1937 case 52: // reg: Load(ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001938 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1939 subtreeRoot->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001940 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001941 break;
1942
1943 case 55: // reg: GetElemPtr(reg)
1944 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001945 // If the GetElemPtr was folded into the user (parent), it will be
1946 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001947 mvec.push_back(new MachineInstr(ADD));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001948 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001949 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001950
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001951 case 57: // reg: Alloca: Implement as 1 instruction:
1952 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001953 AllocationInst* instr =
1954 cast<AllocationInst>(subtreeRoot->getInstruction());
1955 unsigned int tsize =
1956 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001957 assert(tsize != 0);
1958 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001959 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001960 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001961
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001962 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1963 // mul num, typeSz -> tmp
1964 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001965 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001966 AllocationInst* instr =
1967 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001968 const Type* eltType = instr->getAllocatedType();
1969
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001970 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001971 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001972 Value* numElementsVal = NULL;
1973 bool isArray = instr->isArrayAllocation();
1974
1975 if (!isArray ||
1976 isa<Constant>(numElementsVal = instr->getArraySize()))
1977 { // total size is constant: generate code for fixed-size alloca
1978 unsigned int numElements = isArray?
1979 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1980 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1981 numElements, mvec);
1982 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001983 else // total size is not constant.
1984 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001985 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001986 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001987 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001988
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001989 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001990 { // Generate a direct (CALL) or indirect (JMPL). depending
1991 // Mark the return-address register and the indirection
1992 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00001993 // Also, mark the operands of the Call and return value (if
1994 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001995 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001996 // If this is a varargs function, floating point arguments
1997 // have to passed in integer registers so insert
1998 // copy-float-to-int instructions for each float operand.
1999 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002000 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002001 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002002
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002003 // Create hidden virtual register for return address, with type void*.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002004 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002005 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002006 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002007
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002008 // Generate the machine instruction and its operands.
2009 // Use CALL for direct function calls; this optimistically assumes
2010 // the PC-relative address fits in the CALL address field (22 bits).
2011 // Use JMPL for indirect calls.
2012 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00002013 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002014 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002015 M = new MachineInstr(CALL);
2016 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2017 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002018 }
2019 else
2020 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002021 M = new MachineInstr(JMPLCALL);
2022 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2023 callee);
2024 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2025 (int64_t) 0);
2026 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2027 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002028 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002029
Vikram S. Adve74825322002-03-18 03:15:35 +00002030 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002031
Vikram S. Adve242a8082002-05-19 15:25:51 +00002032 const FunctionType* funcType =
2033 cast<FunctionType>(cast<PointerType>(callee->getType())
2034 ->getElementType());
2035 bool isVarArgs = funcType->isVarArg();
2036 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002037
Vikram S. Adve242a8082002-05-19 15:25:51 +00002038 // Use an annotation to pass information about call arguments
2039 // to the register allocator.
2040 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2041 retAddrReg, isVarArgs, noPrototype);
2042 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002043
Vikram S. Adve242a8082002-05-19 15:25:51 +00002044 assert(callInstr->getOperand(0) == callee
2045 && "This is assumed in the loop below!");
2046
2047 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2048 {
2049 Value* argVal = callInstr->getOperand(i);
2050 Instruction* intArgReg = NULL;
2051
2052 // Check for FP arguments to varargs functions.
2053 // Any such argument in the first $K$ args must be passed in an
2054 // integer register, where K = #integer argument registers.
2055 if (isVarArgs && argVal->getType()->isFloatingPoint())
2056 {
2057 // If it is a function with no prototype, pass value
2058 // as an FP value as well as a varargs value
2059 if (noPrototype)
2060 argDesc->getArgInfo(i-1).setUseFPArgReg();
2061
2062 // If this arg. is in the first $K$ regs, add a copy
2063 // float-to-int instruction to pass the value as an integer.
2064 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2065 {
2066 MachineCodeForInstruction &destMCFI =
2067 MachineCodeForInstruction::get(callInstr);
2068 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2069 destMCFI.addTemp(intArgReg);
2070
2071 vector<MachineInstr*> copyMvec;
2072 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2073 callInstr->getParent()->getParent(),
2074 argVal, (TmpInstruction*) intArgReg,
2075 copyMvec, destMCFI);
2076 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2077
2078 argDesc->getArgInfo(i-1).setUseIntArgReg();
2079 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2080 }
2081 else
2082 // Cannot fit in first $K$ regs so pass the arg on the stack
2083 argDesc->getArgInfo(i-1).setUseStackSlot();
2084 }
2085
2086 if (intArgReg)
2087 mvec.back()->addImplicitRef(intArgReg);
2088
2089 mvec.back()->addImplicitRef(argVal);
2090 }
2091
2092 // Add the return value as an implicit ref. The call operands
2093 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002094 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002095 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002096
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002097 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002098 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002099 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002100
Vikram S. Adve74825322002-03-18 03:15:35 +00002101 // delay slot
2102 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002103 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002104 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002105
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002106 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002107 {
2108 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2109 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2110 Instruction* shlInstr = subtreeRoot->getInstruction();
2111
2112 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002113 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2114 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002115
2116 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2117 (opType == Type::LongTy)? SLLX : SLL,
2118 argVal1, argVal2, 0, shlInstr, mvec,
2119 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002120 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002121 }
2122
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002123 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002124 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002125 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2126 "Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002127 mvec.push_back(new MachineInstr((opType->isSigned()
2128 ? ((opType == Type::LongTy)? SRAX : SRA)
2129 : ((opType == Type::LongTy)? SRLX : SRL))));
2130 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002131 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002132 }
2133
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002134 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002135 break; // don't forward the value
2136
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002137 case 71: // reg: VReg
2138 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002139 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002140
2141 default:
2142 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002143 break;
2144 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002145 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002146
Chris Lattner20b1ea02001-09-14 03:47:57 +00002147 if (forwardOperandNum >= 0)
2148 { // We did not generate a machine instruction but need to use operand.
2149 // If user is in the same tree, replace Value in its machine operand.
2150 // If not, insert a copy instruction which should get coalesced away
2151 // by register allocation.
2152 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002153 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002154 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002155 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002156 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002157 Instruction* instr = subtreeRoot->getInstruction();
2158 target.getInstrInfo().
2159 CreateCopyInstructionsByType(target,
2160 instr->getParent()->getParent(),
2161 instr->getOperand(forwardOperandNum),
2162 instr, minstrVec,
2163 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002164 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002165 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002166 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002167 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002168
2169 if (maskUnsignedResult)
2170 { // If result is unsigned and smaller than int reg size,
2171 // we need to clear high bits of result value.
2172 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2173 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002174 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002175 {
2176 unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002177 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002178 { // Mask high bits. Use a TmpInstruction to represent the
2179 // intermediate result before masking. Since those instructions
2180 // have already been generated, go back and substitute tmpI
2181 // for dest in the result position of each one of them.
2182 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2183 NULL, "maskHi");
2184 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2185
2186 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2187 mvec[i]->substituteValue(dest, tmpI);
2188
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002189 M = Create3OperandInstr_UImmed(SRL, tmpI, 4-destSize, dest);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002190 mvec.push_back(M);
2191 }
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002192 else if (destSize < target.DataLayout.getIntegerRegize())
2193 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002194 }
2195 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002196}